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

134
Views
How to ONLY replace the next two characters

I am trying to reset a ISO time to local time, but I am having some trouble converting it.

Currently, I:

// incoming ISO date
let date = new Date('2021-11-08T20:31:23.746Z');

// 1. get timezone offset in hours
const offset = date.getTimezoneOffset() / 60;

// 2. calculate local hour
let localHour = date.toISOString().split('T')[1].substring(0, 2) - offset;

// 3. calculate local time
let localTime = date.toISOString().replace(/[T-:]/, localHour);

console.log(localTime);

// ISO Date
// 2021-11-08T20:31:23.746Z (original)
// 2021-11-08T13:23.746Z (local timezone)

In step 3, I am trying to only replace the first two characters after 'T', however, this is not the correct solution with Regex.

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

0

The Date constructor automatically converts the date to your locale after parsing.

// incoming ISO date
let date = new Date('2021-11-08T20:31:23.746Z');

const res = date.toLocaleString().replaceAll('/', '-').split(', ').join('T')
console.log(res)

// ISO Date
// 2021-11-08T20:31:23.746Z (original)
// 2021-11-08T13:23.746Z (local timezone)

about 4 years ago · Juan Pablo Isaza Report

0

Stop right there and don't do this. Date management is hard. You are going to shoot yourself in the foot. Use a library, for example dayjs.

dayjs(date).local().toDate()
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!