How can you save time by using the built in Date class?

后端 未结 5 1697
后悔当初
后悔当初 2021-01-07 11:57

The intention of this question is to gather solutions to date / time calculation using the built in Date class instead of writing long complicated functions.

I’ll wr

5条回答
  •  误落风尘
    2021-01-07 12:44

    To properly compare to dates you need to use the getTime() function, it will give you the time in milliseconds since January 1, 1970. Which makes it easy to compare to dates, a later date will return a larger value.

    You can subtract one from the other to get the difference, but unfortunately there is no built in time span class to handle this cleanly; you will have to use a bit of math to present the difference properly to the user (eg. dividing the difference with the number milliseconds in a day to get the difference in days).

    var date1:Date = new Date(1994, 12, 24);
    var date2:Date = new Date(1991, 1, 3);
    if(date1.getTime() > date2.getTime())
        trace("date1 is after date2");
    else
        trace("date2 is after or the same as date1");
    

提交回复
热议问题