js实现计算预产期的函数
1.自定义月经周期
2.自定义末次月经时间
实现:预产期日期和当前时间为运气多少周
js引入文件
<script src="yuchanqi.js"></script>
<script src="jisuan.js"></script>
yuchanqi.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;
}
jisuan.js代码:
var $dp,WdatePicker;(function(){var _={
$wdate:true,
$dpPath:"",
$crossFrame:true,
doubleCalendar:false,
enableKeyboard:true,
enableInputMask:true,
autoUpdateOnChanged:null,
whichDayIsfirstWeek:4,
position:{},
lang:"auto",
skin:"default",
dateFmt:"yyyy-MM-dd",
realDateFmt:"yyyy-MM-dd",
realTimeFmt:"HH:mm:ss",
realFullFmt:"%Date %Time",
minDate:"1900-01-01 00:00:00",
maxDate:"2099-12-31 23:59:59",
startDate:"",
alwaysUseStartDate:false,
yearOffset:1911,
firstDayOfWeek:0,
isShowWeek:false,
highLineWeekDay:true,
isShowClear:true,
isShowToday:true,
isShowOK:true,
isShowOthers:true,
readOnly:false,
errDealMode:0,
autoPickDate:null,
qsEnabled:true,
autoShowQS:false,