简体   繁体   中英

How to convert a Date in specific Timezone and send to server

My scenario is a Date object created using the browser timezone (just using new Date() ) and sent to the server, but I want to send this date with another specific timezone let's assume it's, Europe/Athens.

What would be the best representation of the actual date string so I can convert it back to a Date object in the backend in the actual Europe/Athens date?

I have the timezone info but not sure how to get a Fri Feb 05 2021 05:30:00 GMT-0500 (Eastern Standard Time) and convert it to Europe/Athens date.

I have tried to use date-fns but didn't get far.

You've got a few different questions and misconceptions, so I will attempt to address each of them, starting with question in the title:

How to convert a Date in specific Timezone and send to server

  • If you mean a date like 2021-01-18 , that cannot be in a particular time zone. It is, by definition, just a date. Think about a printed calendar you might hang on your wall that has a square for each date. There are no time zones associated with such calendars. One can ask "what date is it now in a particular time zone", but the answer itself has no time zone.

  • If instead you meant a JavaScript Date object, the answer is again "you can't". The Date object is not actually a date, nor does it have a time zone, but rather it is a timestamp . Internally, a Date object only holds one value - the number of milliseconds that have elapsed since 1970-01-01 00:00:00.000 UTC (not considering leap seconds). It has no time zone. Instead, some functions such as .toString() will apply the system-local time zone while operating. That time zone comes from the operating system (in most cases). It is not stored inside the Date object itself.

My scenario is a Date object created using the browser timezone (just using new Date() )...

That will use the system clock where the browser is running, but the time zone is not relevant. The Date object will be constructed from the Unix timestamp that represents "now". Such timestamps are inherently UTC based. The browser fetches the UTC time directly from the operating system, without considering time zone.

... and sent to the server

One cannot send an object to a server without going through deserialization on one side and serialization on the other.

... but I want to send this date with another specific timezone let's assume it's, Europe/Athens.

What would be the best representation of the actual date string so I can convert it back to a Date object in the backend in the actual Europe/Athens date?

Since the object only represents "now", you don't need to send it in a time zone specific format. Just send the UTC time. The ideal format is ISO 8601, which can be obtained directly using the .toISOString() function from the Date object.

Depending on your use case, you might also consider whether you might instead just take the UTC time from the server instead. At least you will have some control over the clock.

On the server side, if you need the time in a specific time zone, then convert from UTC to that time zone on the server, not on the client.

Also, from comments:

I believe even EPOCH is different between timezones. I could just get the EPOCh and then try to work with converting to specific timezones.

That is incorrect on a few levels. First, understand that epoch is just an English word meaning essentially "a representation of a starting point". It isn't a format , nor is it an acronym. In JavaScript, Date objects use Unix timestamps, which use an epoch of 1970-01-01T00:00:00.000Z . In other words, Midnight Jan 1st 1970 UTC is the 0 timestamp. Sometimes, such timestamps are referred to as "epoch time". That's a misnomer in my opinion, but even still - they are always UTC based. There is no time zone, and thus they are the same for the whole world.

Try to use toLocaleString :

let date = (new Date()).toLocaleString("en-US", {timeZone: "Europe/Athens"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM