javascript 如何获取当前时间,然后使用 jquery 比较它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3595403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:38:58  来源:igfitidea点击:

how to get current time and then compare them using jquery

javascriptjquerydatetime

提问by zjm1126

this is my code:

这是我的代码:

    var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

alert(minutes+"/"+hours+"/"+month + "/" + day + "/" + year)

but , i think it hard to compare with two time ,

但是,我认为很难与两次比较,

what can i do ?

我能做什么 ?

thanks

谢谢

回答by jrtipton

If you really want to know whether two Date objects represent precisely the same time, or are before/after one another, it's quite easy: just compare the two Dates via the getTime()method, which returns an integer timestamp for the object. For example,

如果您真的想知道两个 Date 对象是否表示完全相同的时间,或者在另一个之前/之后,这很容易:只需通过getTime()方法比较两个 Dates,该方法返回对象的整数时间戳。例如,

var date1 = myDate,
    date2 = new Date();
return (date1.getTime() < date2.getTime());

would return true if myDate is before 'now', false if it is now or in the future.

如果 myDate 在 'now' 之前,则返回 true,如果是现在或将来,则返回 false。