jQuery/JS - How to compare two date/time stamps?

前端 未结 5 865
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 18:53

I have two date/time stamps:

d1 = 2011-03-02T15:30:18-08:00 
d2 = 2011-03-02T15:36:05-08:00

I want to be above to compare the two:

5条回答
  •  旧巷少年郎
    2020-12-10 19:29

    Seems to work fine for me, although you have to format correctly (i.e. semicolons, quotes):

    var d1 = "2011-03-02T15:30:18-08:00";
    var d2 = "2011-03-02T15:36:05-08:00";
    
    if(new Date(d1) < new Date(d2)) {alert('newer')};
    

    And yes, it takes time into account. If you do this:

    alert(new Date(d1) - new Date(d2));
    

    You get 347000, which is 347 seconds, or 5 minutes, 47 seconds. This is the correct difference between the two.

提交回复
热议问题