문제

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!

도움이 되었습니까?

해결책

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

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

다른 팁

Use toISOString to get a UTC timestamp.

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

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top