这个javascript函数是获取当前时间前后N天日期的方法,可以得到昨天,今天,明天,一月前,三月前,半年前,一年前的日期,只要修改参数就可以实现得到任何一个天数。具体用法如下:
function getDateStr(dayCount,addDayCount) {
var dd = new Date(dayCount);
dd.setDate(dd.getDate()+addDayCount);//获取AddDayCount天后的日期
var y = dd.getFullYear();
var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期,不足10补0
var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号,不足10补0
return y+"-"+m+"-"+d;
}
console.log(getDateStr('2018-10-26',-1) );
console.log("半年前:"+GetDateStr(-180));
console.log("三月前:"+GetDateStr(-90));
console.log("一月前:"+GetDateStr(-30));
console.log("昨天:"+GetDateStr(-1));
console.log("今天:"+GetDateStr(0));
console.log("明天:"+GetDateStr(1));
console.log("后天:"+GetDateStr(2));
console.log("一月后:"+GetDateStr(30));
console.log("三月后:"+GetDateStr(90));
console.log("半年后:"+GetDateStr(180))
function CompareDate(t1,t2)
{
var date = new Date();
var a = t1.split(":");
var b = t2.split(":");
return date.setHours(a[0],a[1]) > date.setHours(b[0],b[1]);
}
调用:
alert( CompareDate("12:00","11:15") );