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

前端 未结 5 2143
梦如初夏
梦如初夏 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:04

    The millisecond value of the time-of-day is going to be the same regardless of your time zone. That is, there are no time zones on planet Earth that differ from one another by a number of milliseconds greater than zero. (They may differ by an integer number of hours or even minutes, but not seconds or milliseconds.)

    That said, the value you get back from getTime() is a UTC-relative timestamp. If two web browsers at widely different spots on the globe create a Date object at the same time, they'll both get the same value from .getTime() (assuming the clocks are synchronized, which is of course highly unlikely).

    Here: 1338585185539 That's a timestamp I just got from my browser. I'm in Austin, TX, and now it's 4:13 in the afternoon (so that timestamp will be from slightly before that). Plug it into a Date instance on your machine and see what it says.

    (edit — for posterity's sake, that timestamp is from 1 June 2012.)

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2021-02-18 19:16

    To get the timestamp from a date in UTC, you need to take in consideration the timezone and daylight savings for that date. For example, a date in January or in July could mean 1 hour difference.

    The option below does just that.

    Date.prototype.getUTCTime = function () {
        return this.getTime() - (this.getTimezoneOffset() * 60000);
    };
    

    It can be used as in:

    var date = new Date();
    var timestamp = date.getUTCTime();
    
    0 讨论(0)
  • 2021-02-18 19:18

    This is an old question but for the sake of the new visitors here is the CORRECT answer:

    Date.now();
    

    It returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC

    0 讨论(0)
  • 2021-02-18 19:25

    how about:

    var now = new Date();
    var utc_now = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());
    console.log('UTC: ' + utc_now) // correct UTC time but wrong timezone!
    console.log('UTC (in ms): ' + utc_now.getTime())
    
    0 讨论(0)
提交回复
热议问题