js日期时间比较函数
1、js日期比较(yyyy-mm-dd)
function duibi(a, b) { var arr = a.split("-"); var starttime = new Date(arr[0], arr[1], arr[2]); var starttimes = starttime.getTime(); var arrs = b.split("-"); var lktime = new Date(arrs[0], arrs[1], arrs[2]); var lktimes = lktime.getTime(); if (starttimes >= lktimes) { alert('开始时间大于离开时间,请检查'); return false; } else return true; }
var yourtime = '2018-06-01 10:35'; yourtime = yourtime.replace("-", "/"); //替换字符,变成标准格式 var d2 = new Date(); //取今天的日期 var d1 = new Date(Date.parse(yourtime)); if (d1 > d2) { alert("开始大于结束"); }
2、js时间比较(yyyy-mm-dd hh:mi:ss)
function comptime() { var beginTime = "2009-09-21 00:00:00"; var endTime = "2009-09-21 00:00:01"; var beginTimes = beginTime.substring(0, 10).split('-'); var endTimes = endTime.substring(0, 10).split('-'); beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19); endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19); alert(beginTime + "aaa" + endTime); alert(Date.parse(endTime)); alert(Date.parse(beginTime)); var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000; if (a < 0) { alert("endTime小!"); } else if (a > 0) { alert("endTime大!"); } else if (a == 0) { alert("时间相等!"); } else { return 'exception' } }
3、当前时间
function CurentTime() { var now = new Date(); var year = now.getFullYear(); //年 var month = now.getMonth() + 1; //月 var day = now.getDate(); //日 var hh = now.getHours(); //时 var mm = now.getMinutes(); //分 var ss = now.getSeconds(); //秒 var clock = year + "-"; if (month < 10) { clock += "0"; } clock += month + "-"; if (day < 10) { clock += "0"; } clock += day + " "; if (hh < 10) { clock += "0"; } clock += hh + ":"; if (mm < 10) { clock += '0'; } clock += mm; return (clock); }
相关文章:JavaScript—当前时间