1

I'm trying to create a date object like this:

const date = new Date('2021-08-02T08:10')
console.log(date, '---', date.toString())
//"2021-08-02T02:40:00.000Z" --- Mon Aug 02 2021 08:10:00 GMT+0530 (India Standard Time)

But this code will create date object which by default has the system timezone embedded to it.

What I want is some way to define a date object in such a way that I keep the date and time same as in the input string, but the timezone of the date object is different. Ex:

const date = new Date('2021-08-02T08:10', { timeZone: 'Asia/Jakarta' })

So that when I log the date object, it gives me data of that timezone

console.log(date.toString()); //Mon Aug 02 2021 08:10:00 GMT+0700 (Western Indonesia Time)

Armostheus
  • 11
  • 1
  • 1
    Hi. It's a good question, but it's been asked many times. See the linked duplicate. TLDR, for now use Luxon, eventually use `Temporal.ZonedDateTime`. Or if you just need a string, use `Intl` (`toLocaleString`, etc.) – Matt Johnson-Pint Aug 03 '21 at 16:22
  • "*…this code will create date object which by default has the system timezone embedded to it.* No it won't. Date objects are just a time value (offset in ms from the ECMAScript epoch, 1970-01-01). The host offset is used for non–UTC (aka "local") calculations and methods, including parsing strings without an offset. – RobG Aug 03 '21 at 21:53

0 Answers0