Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Luxon: how to disregard default timezone in a specific date

I want to generate a Luxon date without taking the Settings.defaultZone into account.

In my case, I got a date string from a third party date picker component. The format is as the following:

2019-06-28T00:00:00

Now, however, we use Luxon through all our app to manage dates, so I need to parse that date to generate a Luxon one.

therefore, I parse the string as following:

import { DateTime } from 'luxon';
function parseDate(dateString) { // Let's say dateString === 2019-06-28T00:00:00

  const formattedDate = DateTime.fromISO(value); // 2019-06-27T23:00:00.000Z
 ...
}

As you can see, the formattedDate is affected by the current timezone. In this specific case, in the application bootstrap, we set the general timezone to GMT+1.

Therefore, the formatted date is set to 27 of june at 23:00, instead of 28 of june at 00:00, which is the date the user selects in the datepicker. The global timezone setting is tweaking with the time.

This is generally great, but in this specific case (the user is picking the expiration date of their id card) we don't need nor want to take timezones into account. I would like the date to be set to day 28 of june with utc timezone.

I tried this:

const formattedValue = DateTime.fromISO(value).setZone('utc');

However this does not modify the date and it is set to 27 of June.

I guess there is an easy way to achieve this, only I cannot find it.

like image 831
Sergeon Avatar asked Jun 04 '19 08:06

Sergeon


1 Answers

I was real close:

const formattedValue = DateTime.fromISO(value, {zone: 'utc'});

Does the trick.

like image 67
Sergeon Avatar answered Oct 14 '22 00:10

Sergeon