It’s often necessary to convert Date into a standardized format that can be easily parsed and understood by different systems. One such format is the ISO string format, which uses a standard notation to represent dates and times in a consistent way.
- To convert a date object into an ISO string, we can use the toISOString() method that is built into the Date object. This method returns a string that represents the date and time in ISO format.
Let see example:
const date = new Date(); const isoString = date.toISOString(); console.log(isoString);
It represents the current date and time in ISO format.
The format of the string is as follows: YYYY-MM-DDTHH:mm:ss.sssZ
Where
YYYY
is the four-digit yearMM
is the two-digit month (with January being01
and December being12
)DD
is two-digit day of the monthT
is an literal character that separates the date and timeHH
is two-digit hour in 24-hour formatmm
is the two-digit minutess
is the two-digit secondsss
is the three-digit millisecondZ
is a literal character that indicates that the time is in UTC
- To convert a ISOString in to date we have to pass ISOString into date object which will return Date
Let see example:
const isoString = "2023-02-17T12:01:39.094Z" const date = new Date(isoString) console.log(date)
Also you can show case ISOString into any format you want here is a blog link below is link for same 👇
https://www.thecodehubs.com/how-to-format-date-and-time-with-moment-js/
Hope you find solution to convert date into ISOString
Feel free to reach in comment section if you have any doubt.