Getting current date in milliseconds (UTC) (NO use of strings)

前端 未结 5 2152
梦如初夏
梦如初夏 2021-02-18 18:35

Well, you might think that this question has already been asked, but I think it has not. The solutions I\'ve read about all had this \"jigsaw puzzle\" technique (like getU

5条回答
  •  春和景丽
    2021-02-18 19:14

    I have used this function to solve the problem.

    function getUTCNow()
    {
        var now = new Date();
        var time = now.getTime();
        var offset = now.getTimezoneOffset();
        offset = offset * 60000;
        return time - offset;
    }
    
    • The getTime function returns the number of milliseconds elapsed since 1 January 1970 00:00:00 in the client timezone.
    • getTimezoneOffset return offset in minutes between Client timezone and UTC.
    • offset = offset * 60000; this operation transform minutes in miliseconds.
    • subtracting the offset get the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

提交回复
热议问题