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

179
Views
How can I get the timestamp in javascript from a string including the timezone db name

I got the following string: "2022/05/01 03:10:00" and I need to create a Date object forcing it to use Chile's UTC offset.

The problem is that because of Daylight saving time (DST) the offset changes twice a year.

How can get that Date object, for example, using the "America/Santiago" TZ db name?

Something like:

new Date("2022/05/01 03:10:00" + getUtcOffset("America/Santiago")).

function getUtcOffset(tzDbName) {
..
}

Returns -3 or -4, depending the time in the year.

EDIT:

I ended using a nice trick for determining if DST was on or off. reference

const dst = hasDST(new Date(strDate));
function hasDST(date = new Date()) {
const january = new Date(date.getFullYear(), 0, 1).getTimezoneOffset();
const july = new Date(date.getFullYear(), 6, 1).getTimezoneOffset();

return Math.max(january, july) !== date.getTimezoneOffset();

}

Then I could create the date with the correct timezone depending on that variable.

if (dst) {
    let d = new Date(strDate + " GMT-0300");
    return d;
} else {
    let d = new Date(strDate + " GMT-0400");
    return d;
}

Thanks everyone!

EDIT2: I finally found a very nice library that does exactly what I was looking for:

https://date-fns.org/v2.28.0/docs/Time-Zones#date-fns-tz

const { zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz')
const utcDate = zonedTimeToUtc('2022-05-05 18:05', 'America/Santiago')
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This has been discussed before here. Haven't tested it, but it appears that the simplest solution is:

// Example for Indian time
let indianTime = new Date().toLocaleTimeString("en-US", 
   {timeZone:'Asia/Kolkata',timestyle:'full',hourCycle:'h24'})
console.log(indianTime)

You can check the link for more complex answers and libraries

Generals notes To get the time zone name use:

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)

To get the difference from UTC (in minutes) use:

var offset = new Date().getTimezoneOffset();
console.log(offset);
// if offset equals -60 then the time zone offset is UTC+01
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!