admin管理员组文章数量:1122846
I'm in a situation where, on the server, I have two values:
- a datetime string like
"2020-01-01T00:00:00"
- a timezone string like
"America/Chicago"
How can I create a UTC Date corresponding to this moment in time?
More details..
In my web application, I'm designing a schedule that looks like this
The user can select a startDate and an endDate to run a query to fetch all the events in that range. In turn, this simply updates the URL to something like
www.foo/user/123/schedule?startDate=2020-01-01&endDate=2020-01-05
In this example, the user is requesting all events between 2020-01-01 00:00:00
and 2020-01-05 11:59:59
in their local time zone.
From the client, I could convert these local timestamps to UTC timestamps and then make the request, like this
www.foo/user/123/schedule?startDate=2020-01-01T06:00:00Z&endDate=2020-01-06T05:59:59Z
BUT
- I think the previous URL format is prettier and easier to understand for the user
- If someone navigates directly to
www.foo/user/123/schedule
, I want the ability to check their cookies for a timeZone string and choose reasonable defaults for startDate and endDate.
I'm in a situation where, on the server, I have two values:
- a datetime string like
"2020-01-01T00:00:00"
- a timezone string like
"America/Chicago"
How can I create a UTC Date corresponding to this moment in time?
More details..
In my web application, I'm designing a schedule that looks like this
The user can select a startDate and an endDate to run a query to fetch all the events in that range. In turn, this simply updates the URL to something like
www.foo.com/user/123/schedule?startDate=2020-01-01&endDate=2020-01-05
In this example, the user is requesting all events between 2020-01-01 00:00:00
and 2020-01-05 11:59:59
in their local time zone.
From the client, I could convert these local timestamps to UTC timestamps and then make the request, like this
www.foo.com/user/123/schedule?startDate=2020-01-01T06:00:00Z&endDate=2020-01-06T05:59:59Z
BUT
- I think the previous URL format is prettier and easier to understand for the user
- If someone navigates directly to
www.foo.com/user/123/schedule
, I want the ability to check their cookies for a timeZone string and choose reasonable defaults for startDate and endDate.
1 Answer
Reset to default 2Since you're only allowing users to select a date, you shouldn't append the T00:00:00
. The ISO format allows for a "local date" type like 2024-11-22
, which does not represent a specific point in time without timezone info, or date-time representations, which should include the offset information to identify a specific moment.
https://en.wikipedia.org/wiki/ISO_8601
You can't do this with plain JavaScript because the standard date library doesn't include zoneinfo (the temporal api will provide this someday...) This will do what you want:
import { DateTime } from 'luxon'
// combining a local date and a zone can identify a specific point in time
// then we convert to UTC
const toUtc = (localDate: string, zone: string) => {
return DateTime.fromISO(localDate, { zone }).toUTC().toJSDate()
}
const utcDate = toUtc('2020-01-01', 'America/Chicago')
// outputs 2020-01-01T06:00:00.000Z
console.log(utcDate.toISOString())
Also note (repeating a bit from my comment above): in your question, you (correctly) used a timezone id, a.k.a. "IANA Time Zone Names". The offset designators like "CT" are meaningless except as a "friendly" format to display to users. These cannot be used to determine a specific point in time because they are not unique. There are over 60 different timezones that do or have previously used "CST".
本文标签:
版权声明:本文标题:javascript - Given a datetime string and a time zone identifier, how can I get the corresponding UTC datetime, server-side? - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300455a1930820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"2020-01-01T00:00:00 CT"
to it corresponding "UTC" timestamp? ("CT" = "Central Time" = "America/Chicago") – Ben Commented Nov 22, 2024 at 23:16/fooEndPoint?startDate=2020-01-01
and a cookie contains their timezone string. – Ben Commented Nov 22, 2024 at 23:53