Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse timezone date

I have the following date string: 2022-03-21 00:00:00.0 CET

I tried this format: yyyy-MM-dd HH:mm:ss.S zzz

I got: RangeError: Invalid time value

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the library "date-fns-tz".

Then you could do something like

import { utcToZonedTime } from 'date-fns-tz';
import { parse } from 'date-fns';

const currentFullDateTZ = '2022-03-21 00:00:00.0 CET';
const fullDate = currentDate.split('.')[0];
const format = 'yyyy-MM-dd HH:mm:ss';

const parsedDate = parse(currentFullDateTZ, format, new Date());
const timeZone = 'CET';
const zonedDate = utcToZonedTime(parsedDate, timeZone);

So basically you first split the date and the timezone and then format the date and apply the timezone.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading