How to Remove T and Z from ISO Dates in JavaScript
To remove the T and Z characters from an ISO date in JavaScript, we first need to have the date in ISO format. If you don't have it already, you can convert it to ISO using the toISOString
function on a Date
object.
new Date().toISOString()
This code will convert your UTC date string to ISO 8601. ISO 8601 comes in the following format:
2022-06-22T07:54:52.657Z
You can use a combination of two replace function calls on the ISO date to remove the T and Z from the original date string. Take a look at the following code example:
new Date().toISOString()
.replace('T', ' ')
.replace('Z', '')
This will get rid of both T and Z, resulting in "2022-06-22 07:54:52.657". Note that you want to replace T with empty whitespace, while you want to replace Z with no character.
In ISO 8601, T is a delimiter used for time, and Z is added to the end to indicate that the date has a zero UTC offset.
Want to learn more on how to format dates in different ways in JavaScript? Check out the "How to Format Dates" section in the following webtip:
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: