Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

luxon convert local time to utc given a timezone

A data source has an ISO-8601 datetime field without an offset.

Example: 2019-07-09T18:45

However, I know that the time in question is understood to be in the America/Chicago timezone.

How can I get a Luxon DateTime object of this time's equivalent in UTC?

I can do DateTime.fromISO('2019-07-09T18:45').plus({hours: 5}) ... but this will only be valid during half of the year (like now) when it is daylight savings time. Otherwise the offset would be .plus({hours: 6})

Does Luxon have a date-aware (and therefore DST-aware) method for converting from a specific zoned local time to UTC?

like image 876
WillD Avatar asked Jul 08 '19 00:07

WillD


People also ask

How do you convert timezone to UTC time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

How do you convert UTC time to local time in node JS?

Use the Date() constructor to convert UTC to local time, e.g. new Date(utcDateStr) . Passing a date and time string in ISO 8601 format to the Date() constructor converts the UTC date and time to local time.

What time is it now UTC 24 hour?

UTC time in ISO-8601 is 11:51:38Z.

How do I get the timezone from DateTime?

How to Get the Current Time of a Timezone with datetime. You can get the current time in a particular timezone by using the datetime module with another module called pytz . You can then check for all available timezones with the snippet below: from datetime import datetime import pytz zones = pytz.


1 Answers

Since you know the timezone of you input date, you can use the zone option when parsing it. As fromISO docs states:

public static fromISO(text: string, opts: Object): DateTime

opts.zone: use this zone if no offset is specified in the input string itself.

Then you can use toUTC to convert your DateTime to UTC:

"Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime.

Equivalent to setZone('utc')

Here a live sample:

const DateTime = luxon.DateTime;
const d = DateTime.fromISO('2019-07-09T18:45', {zone: 'America/Chicago'});
console.log(d.toISO());
console.log(d.toUTC().toISO());
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.js"></script>
like image 62
VincenzoC Avatar answered Nov 13 '22 15:11

VincenzoC