How to Remove T and Z from ISO Dates in JavaScript

How to Remove T and Z from ISO Dates in JavaScript

Convert and format UTC dates
Ferenc Almasi β€’ 2022 June 22 β€’ Read time 1 min read
  • twitter
  • facebook
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.

Copied to clipboard!
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:

Copied to clipboard!
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:

How to Get Tomorrow's Date in JavaScript
  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.