Last updated: Mar 6, 2024
Reading timeยท3 min
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.
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"
We used the
String.split()
method to split the ISO 8601 string on the T
character.
// ๐๏ธ ['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.
// โ 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.
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.
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.
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.
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);
To get an ISO date without the milliseconds:
.
character.0
.Z
to the end of the result.date.toISOString().split('.')[0] + 'Z'
.// โ 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"
The String.split() method splits a string on the provided separator and returns an array of substrings.
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.
const isoStr = '2022-07-21T09:35:31Z'; const withoutMilliseconds = isoStr.includes('.') ? isoStr.split('.')[0] + 'Z' : isoStr; // ๐๏ธ "2022-07-21T09:35:31Z" console.log(withoutMilliseconds);
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.
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.
You can learn more about the related topics by checking out the following tutorials: