0

I have been trying somewhat unsucessfully to convert a date I have in Moment format to a JS Date object that is in the ISO 8601 format. The API library I am using needs the date to be a JS Date object but needs to format to be 8601. When I try this now my moment is the correct time in 24 hour format

Moment {_isAMomentObject: true, _i: '2021-10-07T15:00:00', _f: 'YYYY-MM-DDTHH:mm:ss', _isUTC: true, _pf: {…}, …}
_a: (7) [2021, 9, 7, 15, 0, 0, 0]
_d: Thu Oct 07 2021 11:00:00 GMT-0400 (Eastern Daylight Time) {}
_f: "YYYY-MM-DDTHH:mm:ss"
_i: "2021-10-07T15:00:00"
_isAMomentObject: true
_isUTC: true

but when I convert to a date it tries to fix the timezone so its always off by 4 hours so like momentVar.toDate() =

Thu Oct 07 2021 11:00:00 GMT-0400 (Eastern Daylight Time)

the moment gets set initially through url param:

eventTime=2021-10-07T15:00:00

what is the right get a JS Date object in an ISO 8601 format but not as a string with the .toISOstring() method

depperm
  • 10,606
  • 4
  • 43
  • 67
  • 4
    _"needs the date to be a JS Date object but needs to format to be 8601"_ - that makes no sense to begin with. It is either a Date object instance, or a formatted date _string_ - both at the same time, that doesn't even exist. – CBroe Oct 04 '21 at 13:30
  • 3
    *"The API library I am using needs the date to be a JS Date object but needs to format to be 8601."* "Format" is a textual thing. The API can require that it be a `Date` object, **or** that it be in a particular textual (string) format, but not both. Look at the API documentation to figure out whether you need a `Date` object or a string in the ISO format. – T.J. Crowder Oct 04 '21 at 13:31
  • Sorry it is expecting a format similar to this: eventDateTime: "2021-10-07T19:00:00.000Z" Which is what I am sending when looking at the network tab and works but the timezone is off it should not be 19:00 but 15:00 –  Oct 04 '21 at 13:36
  • 2021-10-07T15:00:00 GMT+00:00 (i.e. UTC) is exactly the same moment in time as Thu Oct 07 2021 11:00:00 GMT-0400, they are just different formats of the same instant with different offsets, the first with offset +0 and the second with offset -4. – RobG Oct 04 '21 at 13:57
  • As explained in other comments, Date objects don't have a format, they are just an offset from the ECMAScript epoch (1970-01-01T00:00:00 UTC). You can create a timestamp in any format you like with any offset you like, see [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1959.7235). – RobG Oct 04 '21 at 14:00

0 Answers0