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

前端 未结 5 2142
梦如初夏
梦如初夏 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: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();
    

提交回复
热议问题