<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>JS计算日期段的天数 </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript">
/*将String类型解析为Date类型.
parseDate('2006-1-1') return new Date(2006,0,1)
parseDate(' 2006-1-1 ') return new Date(2006,0,1)
parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16)
parseDate(' 2006-1-1 15:14:16 ') return new Date(2006,0,1,15,14,16);
parseDate('2006-1-1 15:14:16.254') return new Date(2006,0,1,15,14,16,254)
parseDate(' 2006-1-1 15:14:16.254 ') return new Date(2006,0,1,15,14,16,254)
parseDate('不正确的格式') retrun null
*/
function parseDate(str){
if(typeof str == 'string'){
var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
if(results && results.length>3){
return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]));
}
results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
if(results && results.length>6){
//alert(results[1]+'--'+results[2]+'--'+results[3]+'--'+results[4]+'--'+results[5]+'--'+results[6]);
return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]));
}
results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);
if(results && results.length>7){
return new Date(parseInt(results[1]),parseInt(results[2]) -1,parseInt(results[3]),parseInt(results[4]),parseInt(results[5]),parseInt(results[6]),parseInt(results[7]));
}
}
return null;
}
function transDate(str){
if(typeof str == 'string'){
}
return null;
}
/*
将Date/String类型,解析为String类型.
传入String类型,则先解析为Date类型
不正确的Date,返回 ''
如果时间部分为0,则忽略,只返回日期部分.
*/
function formatDate(v){
if(typeof v == 'string') v = parseDate(v);
if(v instanceof Date){
var y = v.getFullYear();
var m = v.getMonth() + 1;
var d = v.getDate();
var h = v.getHours();
var i = v.getMinutes();
var s = v.getSeconds();
var ms = v.getMilliseconds();
if(ms>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s + '.' + ms;
if(h>0 || i>0 || s>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
return y + '-' + m + '-' + d;
}
return '';
}
function DateDiff(asStartDate,asEndDate)
{ //获得日期差值
var miStart=Date.parse(asStartDate.replace(/\-/g,'/'));
var miEnd=Date.parse(asEndDate.replace(/\-/g,'/'));
return (miEnd-miStart)/(1000*24*3600);
}
function btnCount_Click(){
s1 = document.getElementById('start').value;
s2 = document.getElementById('end').value;
s1=parseDate(s1);
s2=parseDate(s2);
alert(s1+'---------'+s2);
//alert(msToDay(Date.parse(s1)-Date.parse(s2)));
// alert(DateDiff(s1,s2))
}
function msToDay(ms){
var ss = 1000;
var mi = ss * 60;
var hh = mi * 60;
var dd = hh * 24;
var day = ms / dd;
var hour = (ms - day * dd) / hh;
var minute = (ms - day * dd - hour * hh) / mi;
var second = (ms - day * dd - hour * hh - minute * mi) / ss;
var milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
var strDay = day < 10 ? 0 + day : day;
var strHour = hour < 10 ? 0 + hour :hour;
var strMinute = minute < 10 ? 0 + minute :minute;
var strSecond = second < 10 ? 0 + second :second;
var strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
return parseInt(strDay) + "天" + strHour + "小时" + strMinute + "分" + strSecond + "秒";
}
//计算天数差的函数,通用
function DateDiff(sDate1, sDate2){ //sDate1和sDate2是2006-12-18格式
var aDate, oDate1, oDate2, iDays
aDate = sDate1.split("-")
oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]) //转换为12-18-2006格式
aDate = sDate2.split("-")
oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) //把相差的毫秒数转换为天数
return iDays
}
</script>
</HEAD>
<BODY>
<input id="start" type="text" value="2010-08-14 11:11:11"> <input id="end" type="text" value="2010-08-09 11:15:11">
<input name="" type="button" οnclick="btnCount_Click()" value="计算天数" />
减得毫秒,自己再除以分、时、天
</BODY>
</HTML>