0

I have a date like this

Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)

I am running this function on this date

const date = Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)

console.log(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds())

Which returns

2022 9 29 2 0 0

Is it possible to convert these numbers to

2022-10-29T02:00:00.000

the whole point of this is to use a library like moment-timezone and parse 2022-10-29T02:00:00.000 to a new timezone for example new_york. Im doing this to get the UTC time for 2022-10-29T02:00:00.000 in new york. The issue with new Date(2022-10-29T02:00:00.000) is that it always converts it to my local timezone.

KingJoeffrey
  • 303
  • 5
  • 16
  • [`date.toISOString()`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString). See [ISO 8601](//en.wikipedia.org/wiki/ISO_8601). – Sebastian Simon Oct 29 '22 at 03:34
  • date.toISOString() with Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time) returns 2022-10-29T08:00:00.000Z, I want it to return 2022-10-29T02:00:00.000 – KingJoeffrey Oct 29 '22 at 03:41
  • 1
    [Duplicate](//google.com/search?q=site:stackoverflow.com+js+get+local+ISO+date) of [Javascript date format like ISO but local](/q/12413243/4642212). `date.setTime(date.getTime() - date.getTimezoneOffset() * 60_000); date.toISOString().replace("Z", "");`. – Sebastian Simon Oct 29 '22 at 03:53
  • I figured this was the answer, Just wasnt sure if there some other cleaner way to go about it or a built in method. I will use this. THANKS SIMON – KingJoeffrey Oct 29 '22 at 04:09

0 Answers0