Get current timestamp from specific timezone

烈酒焚心 提交于 2019-12-09 17:41:32

问题


is there an easy way to get the unix timestamp in javascript from a specific timezone? for example i want the client send me an unix timestamp but i want to get it to match my timezone.

thanks!


回答1:


Why not simply send the date in UTC, and then convert to your timezone on the server?

var utcEpochSeconds = dateObj.getTime() + (dateObj.getTimezoneOffset() * 60000);



回答2:


Use toISOString to get a UTC timestamp.

var date = new Date();
date.toISOString(); // EST would be 6 hour diff from GMT



回答3:


In order for this to happen, you need to apply the timezone offset to the time, and then remove your offset from value (test this, I am guessing from memory):

var now = new Date(),
    offset = -(now.getTimezoneOffset() * 60 * 1000), // now in milliseconds
    userUnixStamp = +now + offset;

Now offset from your own:

var now = new Date(),
    offset = now.getTimezoneOffset() * 60 * 1000,
    yourUnixStamp = userUnixStamp - offset;


来源:https://stackoverflow.com/questions/9576786/get-current-timestamp-from-specific-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!