javascript 如何在javascript中比较这种格式的两个时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21141323/
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-27 20:11:15 来源:igfitidea点击:
How to compare two timestamps in this format in javascript?
提问by coderman
I have two timestamps formats say,
我有两种时间戳格式说,
a='2014-01-15 00:00:00.0'
b='2014-01-16 00:00:00.0'
How to compare and find if b is after a or not?
如何比较和查找 b 是否在 a 之后?
回答by Dipak Ingole
You may need to do something like this,
你可能需要做这样的事情,
d1 = new Date("2014-01-15 00:00:00.0");
d2 = new Date("2014-01-16 00:00:00.0");
if ((d1 - d2) == 0) {
alert("equal");
} else if (d1 > d2) {
alert("d1 > d2");
} else {
alert("d1 < d2");
}
Working fiddle
工作小提琴
回答by felipekm
var a ='2014-01-15 00:00:00.0';
var b ='2014-01-16 00:00:00.0';
if (b > a) {
// do stuff
}