Is it possible for me to convert
"Thu Oct 27 2022 02:00:00 GMT-0600 (Mountain Daylight Time)"
to this?
"Thu Oct 27 2022 02:00:00 GMT-0400 (New_York or whatever its actually called)"
I am using a component called react-datepicker
My datePicker is returning a date like this, Im in Colorado.
"Thu Oct 27 2022 02:00:00 GMT-0600 (Mountain Daylight Time)"
When i convert this to UTC i get
"2022-10-27T08:00:00Z"
My component is ALWAYS going to return me a time in my local timezone, which is Denver. I would like to be able to select a time, have it return me the first date, then somehow convert that to the second date in New york time.
"Thu Oct 27 2022 02:00:00 GMT-0400 (New_York or whatever its actually called)"
This way when I convert to utc I can get the output below
"2022-10-27T06:00:00Z"
Can anyone help me with this?
CodePudding user response:
Instatiate a new Date
object with the offset. See How to initialize a JavaScript Date to a particular time zone for more specifics.
const date1 = new Date('August 19, 1975 23:15:30 GMT 07:00');
const date2 = new Date('August 19, 1975 23:15:30 GMT-02:00');
CodePudding user response:
Here is how I achieved what I wanted using moment-timezone
//date coming in like this Sat Oct 29 2022 02:00:00 GMT-0600 (Mountain Daylight Time)
const applyOffset = date.setTime(date.getTime() - date.getTimezoneOffset() * 60_000);
const actualTime = new Date(applyOffset).toISOString().replace("Z", "")
const toTz = momentt.tz(actualTime, timezone).format()
const getUTCTime = momentt.utc(toTz).format()
Note timezone variable getting passed to momentt.tz(actualTime, timezone).format() can be any named timezone like "America/New_York" or "America/Chicago"
When I console.log(getUTCTime) I get 2022-10-29T06:00:00Z If I make timezone "America/New_York"