Get ISO Date without Milliseconds or Time in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Table of Contents

  1. Get an ISO Date string without Time using JavaScript
  2. Get ISO Date without the Milliseconds using JavaScript

# Get an ISO Date string without Time using JavaScript

Use the split() method to get an ISO date string without time.

We split the string on the T character and assign the first array element to a variable to get the date component only.

index.js
const date = new Date(); // โœ… If you have a Date object const [withoutTime] = date.toISOString().split('T'); console.log(withoutTime); // ๐Ÿ‘‰๏ธ 2023-07-27 // โœ… If you have a plain ISO string const [withoutT] = '2022-11-14T00:55:31.820Z'.split('T'); console.log(withoutT); // ๐Ÿ‘‰๏ธ "2022-11-14"

get iso date string without time

The code for this article is available on GitHub

We used the String.split() method to split the ISO 8601 string on the T character.

index.js
// ๐Ÿ‘‡๏ธ ['2022-11-14', '00:55:31.820Z'] console.log('2022-11-14T00:55:31.820Z'.split('T'));

The split method returns an array of the substrings split by the provided separator.

We used array destructuring to assign the first array element (the date) to a variable.

index.js
// โœ… If you have a plain ISO string const [withoutT] = '2022-11-14T00:55:31.820Z'.split('T'); console.log(withoutT); // ๐Ÿ‘‰๏ธ "2022-11-14"

Here is an example that shows how array destructuring works.

index.js
const [one, two] = [1, 2]; console.log(one); // ๐Ÿ‘‰๏ธ 1 console.log(two); // ๐Ÿ‘‰๏ธ 2

The variables to the left of the equal sign get assigned the array elements in the same order.

You can also use a comma to skip an array element if you don't need it.

index.js
const [, two] = [1, 2]; console.log(two); // ๐Ÿ‘‰๏ธ 2

The withoutT variable stores a date formatted as YYYY-MM-DD.

You can use the date to create a new Date object.

index.js
const [withoutT] = '2022-11-14T00:55:31.820Z'.split('T'); console.log(withoutT); // ๐Ÿ‘‰๏ธ "2022-11-14" const newDate = new Date(withoutT); // ๐Ÿ‘‡๏ธ Mon Nov 14 2022 02:00:00 console.log(newDate);

Note that if you create a date without specifying the time, you get a date set in UTC.

If you want to create a date in the visitor's local time, include the time.

index.js
const [withoutT] = '2022-11-14T00:55:31.820Z'.split('T'); console.log(withoutT); // ๐Ÿ‘‰๏ธ "2022-11-14" const newDate = new Date(withoutT + 'T00:00:00'); // ๐Ÿ‘‡๏ธ Mon Nov 14 2022 00:00:00 console.log(newDate);

# Get ISO Date without the Milliseconds using JavaScript

To get an ISO date without the milliseconds:

  1. Split the ISO string on the dot . character.
  2. Access the array element at index 0.
  3. Append the letter Z to the end of the result.
  4. For example, date.toISOString().split('.')[0] + 'Z'.
index.js
// โœ… If you have a Date object const date = new Date(); const withoutMs = date.toISOString().split('.')[0] + 'Z'; console.log(withoutMs); // ๐Ÿ‘‰๏ธ "2023-07-27T05:08:50Z" // โœ… If you have a plain ISO string const isoStr = '2022-07-21T09:35:31.820Z'; const withoutMilliseconds = isoStr.split('.')[0] + 'Z'; console.log(withoutMilliseconds); // ๐Ÿ‘‰๏ธ "2022-07-21T09:35:31Z"

get iso date without milliseconds

The code for this article is available on GitHub

The String.split() method splits a string on the provided separator and returns an array of substrings.

index.js
const isoStr = '2022-07-21T09:35:31.820Z'; // ๐Ÿ‘‡๏ธ ['2022-07-21T09:35:31', '820Z'] console.log(isoStr.split('.'));

ISO formatted dates look like: YYYY-MM-DDTHH:mm:ss.sssZ, so we can split the string on the dot . that separates the seconds from the milliseconds.

The last step is to access the array element at index 0 and append the Z character.

If your ISO strings might not contain the dot and milliseconds for some reason, it's best to use a ternary operator to check before splitting on a non-existent character.
index.js
const isoStr = '2022-07-21T09:35:31Z'; const withoutMilliseconds = isoStr.includes('.') ? isoStr.split('.')[0] + 'Z' : isoStr; // ๐Ÿ‘‡๏ธ "2022-07-21T09:35:31Z" console.log(withoutMilliseconds);

using ternary operator to check before splitting

We used the String.includes() method to check if the ISO string contains a dot . character.

The ternary operator is very similar to in if/else statement.

If the condition to the left of the question mark returns a truthy value, the value to the left of the colon is returned, otherwise, the value to the right is returned.

In other words, if the ISO string contains a dot, we split the string and remove the milliseconds part, otherwise we return the string as is.

You only have to do this if your ISO string might not contain the milliseconds part, e.g. because you already removed it, or it was never there to begin with.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev