I have a timestamp
with this format 09:53:56,07-04-2021
How do I convert it to ISO
format ?
I have tried
moment("09:53:56,07-04-2021").format("hh:mm:ss,mm-dd-yyyy")
I'm getting "Invalid date"
message
There are two separate function calls, always executed consequently, in your code. The first of those...
moment("09:53:56,07-04-2021")
... is parsing, when Moment constructor tries to create an object from the string parameter it gets. The next call is formatting:
.format("hh:mm:ss,mm-dd-yyyy")
... when Moment uses the string argument to prepare and return a proper representation of its internal date object.
The key is that the first call cannot use the data of the second one. That's why it actually doesn't matter which format you pass there. By the time it's called Moment already misinterpreted the string - and created a messed up date object out of it.
What you need is supplying the correct format into Moment constructor as its second param, like described in the docs. There are two options:
moment('09:53:56,07-04-2021', 'HH:mm:ss,MM-DD-YYYY')
moment.utc('09:53:56,07-04-2021', 'HH:mm:ss,MM-DD-YYYY')
The last one should be used if the datetime provided is always in GMT.
Note that mm
in format string means "minutes"; you need M
or MM
(uppercase) to refer to the "month number" instead. In this case, it's MM, as it's clear that two digits are used.