【问题标题】:How to compare two timestamps in this format in javascript?如何在javascript中比较这种格式的两个时间戳?
【发布时间】:2014-02-04 03:39:15
【问题描述】:

我有两种时间戳格式,

a='2014-01-15 00:00:00.0'
b='2014-01-16 00:00:00.0'

如何比较b是否在a之后?

【问题讨论】:

  • 在这种情况下,简单的字符串比较不应该足够吗?

标签: javascript date comparison


【解决方案1】:

你可能需要做这样的事情,

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

【讨论】:

    【解决方案2】:
    var a ='2014-01-15 00:00:00.0';
    var b ='2014-01-16 00:00:00.0';
    if (b > a) {
    // do stuff
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 2020-06-18
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多