简体   繁体   English

如何从客户端获取的日期获取时区偏移量?

[英]How to get timezone offset from the date getting from client side?

I am working with nodejs.我正在使用nodejs。 I am getting the startDate as我将startDate作为

startDate: '2020-06-14T18:30:00.000Z'

from the client side.从客户端。 I need to get the timezone offset from the above date.我需要从上述日期获取时区偏移量。 It should be +05:30.应该是 +05:30。 How can I get that?我怎么能得到那个?

Edit:编辑:

Server always works on UTC.服务器始终在 UTC 上工作。 So can I get the timezone offset by subtracting the date coming from the client by the UTC one?那么我可以通过 UTC 减去来自客户端的日期来获得时区偏移量吗?

ie IE

'2020-06-14T18:30:00.000Z' - moment.utc().startOf("day") = +05:30

If the node.js syntax is the same as plain JS;如果 node.js 语法和普通 JS 一样; declare a date object with:声明一个日期 object :

var date = new Date();

then use the getTimezonOffset method to get the difference in minutes to GMT.:然后使用getTimezonOffset方法获取与 GMT 的分钟差。:

var difInMinutesToGMT = date.getTimezoneOffset();

From there on it's a matter of converting the positive/negative minutes into the format you want.从那里开始,将正/负分钟转换为您想要的格式。

the date you have is in universal central timezone UTC supposing you have that datestring on your client, you can use from moment.js moment.utc(datestring).utcOffset()您拥有的日期位于通用中央时区 UTC 假设您的客户端上有该datestring ,您可以从moment.js使用moment.utc(datestring).utcOffset()

to get the offset from that string, I think the return value is in minutes.要从该字符串中获取偏移量,我认为返回值以分钟为单位。

Edit: thank you for clarifying, the Z at the end of your datestring means that the date is already in UTC time zone.编辑:感谢您的澄清,日期字符串末尾的 Z 表示日期已经在 UTC 时区。 If you want to have a timezone offset, you will always get 0. you could possibly send as well the locale from the client using moment.tz.guess() and send the timezone as well to the server.如果你想有一个时区偏移,你总是会得到 0。你也可以使用 moment.tz.guess() 从客户端发送区域设置并将时区发送到服务器。 or simply like others have stated, use on client side the Date.prototype.getTimezoneOffset method:或者就像其他人所说的那样,在客户端使用 Date.prototype.getTimezoneOffset 方法:

The getTimezoneOffset() method returns the time zone difference, in minutes, from current locale (host system settings) to UTC. getTimezoneOffset() 方法返回从当前区域设置(主机系统设置)到 UTC 的时区差异(以分钟为单位)。

You can use this below method in order to get the offset in the required format.您可以使用以下方法来获取所需格式的偏移量。

 let d = '2020-06-14T18:30:00.000Z' let date = new Date(d); let offsetInMinutes = date.getTimezoneOffset() let formattedOffset = `${parseInt(offsetInMinutes/60)}:${Math.abs(parseInt(offsetInMinutes%60))}` console.log(formattedOffset)

You cannot get the user's time zone.您无法获取用户的时区。

User/Client sent you date-time in ISO8601 format, or to be more precise Zulu time (UTC).用户/客户以 ISO8601 格式向您发送日期时间,或者更准确地说是祖鲁时间 (UTC)。

You might consider 2 options:您可以考虑 2 个选项:

  1. Forcing clients to send you time in a format which will include their timezone and grab it from there ( '2020-06-14T19:30:00.000+01:00' ) - more like a workaround强制客户以包含他们的时区的格式向您发送时间并从那里获取时间 ( '2020-06-14T19:30:00.000+01:00' ) - 更像是一种解决方法
  2. Or to have user preferences in you database where you will have their zone (somehow stored there, via dedicated endpoint or so...) - cleaner solution或者在您的数据库中拥有用户偏好,您将在其中拥有他们的区域(以某种方式存储在那里,通过专用端点等......) - 更清洁的解决方案

Answering to edit:回复编辑:

moment(''2020-06-14T18:30:00.000Z').substract(moment.utc().startOf("day")).toHours() == 18.5



moment('2020-06-14T19:30:00.000+01:00').substract(moment.utc().startOf("day")).toHours() == 18.5

So, the timezone flag (Z, 01:00...) will just give info about timezone of given date-time.因此,时区标志 (Z, 01:00...) 只会提供有关给定日期时间的时区的信息。 The client can send any of variation and it's valid.客户端可以发送任何变化并且它是有效的。 And in the end you still don't know clients timezone.最后,您仍然不知道客户时区。

My 1st is just a workaround, but I wouldn't suggest you do that, that might be a hotfix, but 2nd is a more appropriate solution.我的第一个只是一种解决方法,但我不建议你这样做,这可能是一个修补程序,但第二个是一个更合适的解决方案。

Anyway:反正:

Sometimes, server/API/be doesn't have to know users timezone at all, if that is used just for read/write (because client app, will adjust ZULU time to the client zone in the UI and vice versa), but when you have some logic, for example google calendar features you will need user timezone for sure.有时,server/API/be 根本不需要知道用户的时区,如果那只是用于读/写(因为客户端应用程序,会将 ZULU 时间调整为 UI 中的客户端区域,反之亦然),但是当你有一些逻辑,例如google calendar features ,你肯定需要用户时区。

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

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