Explanation for Timespan Differences Between C# and JavaScript

后端 未结 2 1313
无人及你
无人及你 2021-02-14 09:41

This is based on Computing milliseconds since 1970 in C# yields different date than JavaScript and C# version of Javascript Date.getTime().

For all of these calculation

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-14 10:17

    I understand that JavaScript Date objects are based on the Unix Epoch (Midnight on Jan 1, 1970).

    Yes, they are. Internally, it's just a number of milliseconds from the epoch. But when you call the date constructor, or look at the output from .toString(), it is using the local time of where the code is running.

    If you want the input to be specified in UTC, then you have to use a different incantation:

    var ts = Date.UTC(2014,1,28);  // returns a numeric timestamp, not a Date object
    
    var dt = new Date(ts);         // if you want a date object
    
    var s = dt.toUTCString();      // if you want the output to be in UTC
    

提交回复
热议问题