Skip to main content

How to Get an ISO Date String Without Time or Milliseconds in JavaScript

The Date.prototype.toISOString() method is the standard way to get a UTC-based, machine-readable date string (e.g., 2023-10-27T10:30:00.123Z). However, you often need to truncate this string to a less precise format, such as removing the time or the milliseconds.

This guide will teach you the simple and reliable string manipulation techniques to achieve these common formatting goals.

Goal 1: Get the Date Only (YYYY-MM-DD)

The ISO 8601 standard uses a T character to separate the date portion from the time portion. This provides a reliable delimiter for us to split the string.

Problem: we have a full ISO string and need to extract only the date part.

// Problem: How to get "2023-10-27" from this string?
const fullISOString = '2023-10-27T10:30:00.123Z';

The String.prototype.split() method is the most direct and readable way to solve this. It splits the string into an array at the T and we can take the first element.

const date = new Date();
const isoString = date.toISOString(); // for example: "2023-10-27T10:30:00.123Z"

// Split the string at the 'T' and take the first part
const dateOnly = isoString.split('T')[0];

console.log(dateOnly); // Output: "2023-10-27"
note

This method is simple, robust, and easy to understand.

Goal 2: Get the Date and Time Without Milliseconds

Sometimes you need to preserve the time but remove the fractional seconds for systems that don't support that level of precision.

Problem: we have a full ISO string and need to remove the milliseconds part.

// Problem: How to get "2023-10-27T10:30:00Z" from this string?
const fullISOString = '2023-10-27T10:30:00.123Z';

The milliseconds are separated from the seconds by a dot (.). We can use this as our delimiter.

const date = new Date();
const isoString = date.toISOString(); // for example: "2023-10-27T10:30:00.123Z"

// Split the string at the '.' and take the first part
const withoutMilliseconds = isoString.split('.')[0] + 'Z';

console.log(withoutMilliseconds); // Output: "2023-10-27T10:30:00Z"
note

Important: We must append the Z back to the end of the string to ensure it remains a valid and correctly-interpreted UTC timestamp.

A more robust version of this would handle cases where the string might not have milliseconds to begin with:

function removeMilliseconds(isoString) {
if (isoString.includes('.')) {
return isoString.split('.')[0] + 'Z';
}
return isoString;
}

console.log(removeMilliseconds('2023-10-27T10:30:00.123Z')); // Works
console.log(removeMilliseconds('2023-10-27T10:30:00Z')); // Also works

A Note on Manual Formatting

While string manipulation of the toISOString() output is very common and effective, an alternative is to build the date string manually. This can be useful if you need the date formatted in the user's local timezone instead of UTC.

function getLocalDateString(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');

return `${year}-${month}-${day}`;
}

const myDate = new Date();
console.log(getLocalDateString(myDate)); // Output: "2023-10-27" (based on local date)

Conclusion

Truncating an ISO date string is a simple task if you use the built-in String.prototype.split() method.

  • To get the date only (YYYY-MM-DD), split the string by the T character: isoString.split('T')[0]
  • To get the date and time without milliseconds, split the string by the . character and re-append the Z: isoString.split('.')[0] + 'Z'

These simple and readable one-liners are the standard way to handle these common formatting needs.