简体   繁体   English

如何在特定时区转换日期并发送到服务器

[英]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.我的场景是使用浏览器时区(仅使用new Date() )创建的日期 object 并发送到服务器,但我想将此日期与另一个特定时区一起发送,假设它是欧洲/雅典。

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?实际日期字符串的最佳表示形式是什么,以便我可以在实际欧洲/雅典日期的后端将其转换回日期 object?

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.我有时区信息,但不确定如何获取Fri Feb 05 2021 05:30:00 GMT-0500 (Eastern Standard Time)并将其转换为欧洲/雅典日期。

I have tried to use date-fns but didn't get far.我曾尝试使用 date-fns 但没有走远。

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.如果您的意思是像2021-01-18这样的日期,则不能在特定时区。 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".相反,如果您的意思是 JavaScript Date object,那么答案又是“您不能”。 The Date object is not actually a date, nor does it have a time zone, but rather it is a timestamp . Date object 实际上不是日期,也没有时区,而是时间戳 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).在内部, Date object 仅保存一个值 - 自 1970-01-01 00:00:00.000 UTC 以来经过的毫秒数(不考虑闰秒)。 It has no time zone.它没有时区。 Instead, some functions such as .toString() will apply the system-local time zone while operating.相反,一些函数,如.toString()将在运行时应用系统本地时区。 That time zone comes from the operating system (in most cases).该时区来自操作系统(在大多数情况下)。 It is not stored inside the Date object itself.它不存储在Date object 本身内。

My scenario is a Date object created using the browser timezone (just using new Date() )...我的场景是使用浏览器时区创建的 Date object (仅使用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". Date object 将从代表“现在”的 Unix 时间戳构造。 Such timestamps are inherently UTC based.这样的时间戳本质上是基于 UTC 的。 The browser fetches the UTC time directly from the operating system, without considering time zone.浏览器直接从操作系统获取 UTC 时间,不考虑时区。

... 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.如果不经过一侧的反序列化和另一侧的序列化,就无法将object发送到服务器。

... 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?实际日期字符串的最佳表示形式是什么,以便我可以在实际欧洲/雅典日期的后端将其转换回日期 object?

Since the object only represents "now", you don't need to send it in a time zone specific format.由于 object 仅代表“现在”,因此您无需以时区特定格式发送。 Just send the UTC time.只需发送UTC时间。 The ideal format is ISO 8601, which can be obtained directly using the .toISOString() function from the Date object.理想的格式是 ISO 8601,可以直接使用Date object 中的.toISOString() function 获得。

Depending on your use case, you might also consider whether you might instead just take the UTC time from the server instead.根据您的用例,您可能还考虑是否可以改为从服务器获取 UTC 时间。 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.在服务器端,如果您需要特定时区的时间,则在服务器上从 UTC 转换为该时区,而不是在客户端上。

Also, from comments:另外,来自评论:

I believe even EPOCH is different between timezones.我相信即使是 EPOCH 在时区之间也是不同的。 I could just get the EPOCh and then try to work with converting to specific timezones.我可以得到 EPOCh,然后尝试转换到特定的时区。

That is incorrect on a few levels.这在几个层面上是不正确的。 First, understand that epoch is just an English word meaning essentially "a representation of a starting point".首先,要理解epoch只是一个英文单词,本质上是“一个起点的表示”。 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 .在 JavaScript 中, Date对象使用 Unix 时间戳,该时间戳使用1970-01-01T00:00:00.000Z的纪元。 In other words, Midnight Jan 1st 1970 UTC is the 0 timestamp.换句话说,UTC 时间 1970 年 1 月 1 日午夜是0时间戳。 Sometimes, such timestamps are referred to as "epoch time".有时,此类时间戳被称为“纪元时间”。 That's a misnomer in my opinion, but even still - they are always UTC based.在我看来,这是用词不当,但即便如此 - 它们始终基于 UTC。 There is no time zone, and thus they are the same for the whole world.没有时区,因此它们对于整个世界都是一样的。

Try to use toLocaleString :尝试使用toLocaleString

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将特定日期转换为悉尼时区 - Convert a specific date into Sydney timezone 如何将特定时区的UTC日期转换为UTC +0(默认格林威治)? - How to convert UTC date of specific timezone to UTC +0 (default Greenwich)? 将没有时区的日期字符串转换为特定时区时刻 object - Convert date string without timezone to specific timezone moment object 如何将日期字符串转换为带时区的日期? - How to convert a date string into date with timezone? 如何使用moment.js和moment-timezone.js将特定时区(非本地)的给定日期/时间转换为UTC - How to convert a given date/time for a specific timezone (not local) to UTC using moment.js and moment-timezone.js 如何使用javascript将本地日期时间转换为UTC并将UTC转换为特定时区? - How to convert Local Date Time to UTC and UTC to specific TimeZone using javascript? 如何将日期从一个时区转换为另一个时区 - How to convert date from one timezone to another timezone 如何在 Javascript 中将 PST 时区日期转换为本地时区? - How to convert PST timezone date to Local TimeZone in Javascript? JavaScript 如何将欧洲/柏林时区转换为本地时区日期 - JavaScript how to convert Europe/Berlin timezone to local timezone date 如何将日期字符串转换为不带时区的日期对象 - How to convert a datestring to date object without timezone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM