js日期时间比较函数

时间:2022-06-03 18:09:16

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;
}

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);
}