How does JS date comparison work?

前端 未结 3 1124
悲&欢浪女
悲&欢浪女 2020-12-17 00:45

Let\'s assume I have a proper Date object constructed from the string: \"Tue Jan 12 21:33:28 +0000 2010\".

var dateString = \"Tue          


        
3条回答
  •  清歌不尽
    2020-12-17 01:44

    Date values in Javascript are numbers, as stated in the ECMA Script specification. So the Date values are compared as numbers.

    This is a demo of your code (I set twitterDate in the future).

    (function(){
        var dateString = "Tue Jan 12 21:33:28 +0000 2014";
        var twitterDate = new Date(dateString);
    
        var now = new Date();
        if (now < twitterDate) {
             document.write('twitterDate is in the future');
        }
        else
        {
            document.write('twitterDate is NOT in the future');
        }
    
    })()​
    

提交回复
热议问题