admin管理员组

文章数量:1122846

I'm in a situation where, on the server, I have two values:

  1. a datetime string like "2020-01-01T00:00:00"
  2. 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

  1. I think the previous URL format is prettier and easier to understand for the user
  2. 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:

  1. a datetime string like "2020-01-01T00:00:00"
  2. 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

  1. I think the previous URL format is prettier and easier to understand for the user
  2. 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.
Share Improve this question edited Nov 23, 2024 at 2:11 Ben asked Nov 22, 2024 at 22:52 BenBen 21.5k34 gold badges126 silver badges211 bronze badges 7
  • ? regardless of their geographical location, all servers are in UTC Zero... – Mister Jojo Commented Nov 22, 2024 at 23:06
  • @MisterJojo I understand. I guess you're confused by my question.. Let me put it like this.. "How do I convert "2020-01-01T00:00:00 CT" to it corresponding "UTC" timestamp? ("CT" = "Central Time" = "America/Chicago") – Ben Commented Nov 22, 2024 at 23:16
  • @Yogi, the date is not stored in a database. It comes from a request from the user/client. The request is formatted like /fooEndPoint?startDate=2020-01-01 and a cookie contains their timezone string. – Ben Commented Nov 22, 2024 at 23:53
  • @Yogi link above is the correct way. You can't do this directly with JavaScript because it doesn't have zoneinfo information. Also note: 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". – Eric Haynes Commented Nov 23, 2024 at 1:30
  • @Yogi please see the update to my post. Thanks! – Ben Commented Nov 23, 2024 at 1:42
 |  Show 2 more comments

1 Answer 1

Reset to default 2

Since 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".

本文标签: