0

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')
joaku
  • 1
  • 1
  • 1
    Very similar to [*get timezone offset of another timezone in javascript without using Strings*](https://stackoverflow.com/questions/35888335/get-timezone-offset-of-another-timezone-in-javascript-without-using-strings) and [*Calculate Timezone offset only for one particular timezone*](https://stackoverflow.com/questions/61361914/calculate-timezone-offset-only-for-one-particular-timezone). – RobG May 01 '22 at 09:10
  • Please provide enough code so others can better understand or reproduce the problem. – Community May 01 '22 at 12:53
  • @joaku - Regarding your edit, that will assume the DST rules for the *local* time zone, not necessarily `America/Santiago`. If you are only interested in the local time zone, then you don't need any of this at all, just do `new Date("2022-05-01T03:10:00")`. And if you *do* need it to be in a specific time zone - not the local zone, the current solutions require a library like [Luxon](https://moment.github.io/luxon/#/) (or others). Eventually this will be doable without a library using [Temporal](https://tc39.es/proposal-temporal/docs/). – Matt Johnson-Pint May 02 '22 at 20:30

1 Answers1

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
lior bakalo
  • 440
  • 2
  • 9
  • 1
    That isn't what the OP Is asking. What they want is to parse a timestamp for a particular location, i.e. it must use the offset for that location for the specified date and time. This answer gets the current IANA representative location from the host system system settings and the current ECMAScript offset in minutes. – RobG May 01 '22 at 09:04
  • You right, didn't notice that. Answer edited – lior bakalo May 01 '22 at 09:12
  • That still doesn't work as it parses the string initially in the host timezone, not the desired timezone. – RobG May 01 '22 at 21:34