How come my javascript (node.js) is giving me the incorrect timestamp?

前端 未结 4 1609
攒了一身酷
攒了一身酷 2021-01-12 02:39

I typed \"date\" in console...and I get Tue Sep 20 01:01:49 PDT 2011 ...which is correct.

But then I do this in node.js, and I get the wrong time.

4条回答
  •  醉酒成梦
    2021-01-12 03:27

    The reason is that the getTime function returns the time in the UTC timezone:

    The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC. You can use this method to help assign a date and time to another Date object.

    If you want to fetch the UNIX timestamp in you current timezone, you can use the getTimezoneOffset method:

    var date = new Date();
    var ts = String(Math.round(date.getTime() / 1000) + date.getTimezoneOffset() * 60);
    

提交回复
热议问题