Skip to content

Instantly share code, notes, and snippets.

@jakeauyeung
Last active August 29, 2015 14:02
Show Gist options
  • Save jakeauyeung/c214586c15a0f2ee1fda to your computer and use it in GitHub Desktop.
Save jakeauyeung/c214586c15a0f2ee1fda to your computer and use it in GitHub Desktop.
计算2个日期的相差天数
Date.prototype.Format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
function DateDiff(sDate1, sDate2) {
var aDate, oDate1, oDate2, iDays;
oDate1 = new Date(sDate1.replace(/-/g, "/"));
oDate2 = new Date(sDate2.replace(/-/g, "/"));
oDate1 = new Date(oDate1);
oDate2 = new Date(oDate2);
iDays = parseInt((oDate1.getTime() - oDate2.getTime()) / (1000 * 60 * 60 * 24)); //把相差的毫秒数转换为天数
return iDays; //返回相差天数
}
var currentTime = new Date().Format("yyyy-MM-dd");
console.log(currentTime);
console.log(DateDiff(currentTime, "2014-06-11"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment