Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Extract local time and timezone offset information using date-fns

I have the following date string: 2022-04-16T18:31:00+02:00 and I would like to extract the local time i.e. 2022-04-16T18:31 and offset information +2:00 or 2 using date-fns.

I know I can do it with a simple string manipulation like this:

const [dateTime, offset] = "2022-04-16T18:31:00+02:00".split('+')

However, if possible, I would like to avoid string manipulation and use the library instead.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I can see not many people is answering here so, here is how you can get date and time in Javascript with this kind of string

const [dateTime, offset] = "2022-04-16T18:31:00+02:00".split('+');

var newDate = dateTime;

//above variable newDate will hold value that is => 2022-04-16T18:31:00

//don't use split('+'); so you can get Timezone as well 



getTimeZone =  "2022-04-16T18:31:0002:00";
var getTimeZone = getTimeZone.substring(19, getTimeZone.length-0);


console.log("timezone is "+ getTimeZone);

`````
about 4 years ago · Juan Pablo Isaza Report

0

I think you should look from date-fns-tz.

Check the method (utcToZonedTime, getTimezoneOffset).

Hope this help 👋🏽

about 4 years ago · Juan Pablo Isaza Report

0

You can try something like this... [untested code]

import { format } from "date-fns";

const d = Date.parse("2022-04-16T18:31:00+02:00");
const outputDate = `${format(d, "yyyy-MM-dd'T'HH:mm")}`;

console.log(outputDate);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!