js计算怀孕周期的方法
js计算怀孕周期的方法,js计算怀孕周期代码教程如下:
随机末次月经时间
实现结果:计算出目前处于孕期多少周和预产期日期
js实现代码如下:
function yunchan() {
var year = document.getElementById('wyear');
var month = document.getElementById('wmonth');
var day = document.getElementById('wday');
if (year.value == "" || month.value == "" || day.value == "") {
alert("璇峰~鍐欏畬鏁村勾鏈堟棩!");
return;
}
if (year.value > 2050 || year.value < 1990) {
alert("璇峰~鍐欐纭勾浠�!");
year.focus();
return;
}
if (month.value > 12 || month.value < 1) {
alert("璇峰~鍐欐纭湀浠�!");
month.focus();
return;
}
if (day.value > 31 || day.value < 1) {
alert("璇峰~鍐欐纭棩瀛�!");
day.focus();
return;
}
if (!isDate(year.value, month.value, day.value)) {
alert("骞存湀鏃ョ粍鍚堟湁閿�,璇烽噸鏂板~鍐�!");
return;
}
function isDate(year, month, day) {
// month argument must be in the range 1 - 12
month = month - 1; // javascript month range : 0- 11
var tempDate = new Date(year, month, day);
if ((year2k(tempDate.getFullYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate())) {
return true;
} else {
return;
}
}
function year2k(d) {
return (d < 1000) ? d + 1900 : d;
}
var stime = new Date();
stime.setFullYear(year.value);
stime.setMonth(month.value - 1);
stime.setDate(day.value);
var yue = document.getElementById('wperiod').value;
var true_number = 280;
if (yue < 28) {
true_number = 280 - (28 - yue);
} else if (yue > 28) {
true_number = 280 + (yue - 28);
} else if (yue == 28) {
true_number = 280;
}
if (yue.value != '') {
var temptime = stime.getTime();
stime.setTime((stime.getTime() + true_number * 24 * 3600 * 1000))
var yuchan = stime.getFullYear() + "-" + (stime.getMonth() + 1) + "-" + stime.getDate();
var nowt = new Date();
var chatime = nowt.getTime() - temptime;
var chaweek = Math.floor((chatime) / (1000 * 60 * 60 * 24 * 7));
var chaweek_1 = Math.floor((chatime) / (1000 * 60 * 60 * 24));
if (chaweek < 0) chaweek = 0;
if (chaweek_1 > (true_number + 14)) {
alert("浣犵殑棰勪骇鏈熷凡杩�!");
return null;
}
document.getElementById('wtext').innerHTML = '鎮ㄧ殑棰勪骇鏈熸槸 <b class="red">'+yuchan+'</b>锛涚幇鍦ㄦ澶勪簬瀛曟湡鐨勭 <b class="red">'+chaweek+'</b> 鍛�';
document.getElementById('wresult').style.display = '';
var c = document.getElementById('wresult').innerHTML;
var i = '';
return {c:c, i:i};
} else {
return null;
}
}
function wInit() {
document.getElementById('wresult').style.display = 'none';
var d = new Date();
d.setMonth(d.getMonth() - 8);
document.getElementById('wyear').value = d.getFullYear();
document.getElementById('wmonth').value = d.getMonth() + 1;
document.getElementById('wday').value = d.getDate();
document.getElementById('wyear').onclick = function(event) {WdatePicker({dateFmt:'yyyy',minDate:'1901', maxDate:'2050'})};
document.getElementById('wmonth').onclick = function(event) {WdatePicker({dateFmt:'M'})};
document.getElementById('wday').onclick = function(event) {WdatePicker({dateFmt:'d'})};
var p = document.getElementById('wperiod');
for (var i = 20; i <= 45; i++) {
p.options.add(new Option(i + '澶�', i));
}
p.options[8].selected = true;
}
function handleWork(obj) {
var v = obj.value;
obj.disabled = true;
obj.value = '寮€濮嬭绠�';
// global callback
handleBegin();
var res = yunchan();
if (typeof res == 'object' && res != null) {
// global callback
handleFinish(res.c, res.i);
}
obj.value = v;
obj.disabled = false;
}
显示效果:怀孕周期计算器
参考文章:http://tishang.net/articles/1685610399.html