Getting year-month-date from JS dates
By Flavio
Published · Updated
Tagged: JavaScript
Learn how to get a YYYY-MM-DD string from a JavaScript Date, in UTC, in local time, or in a specific time zone, and avoid the off-by-one-day bugs near midnight.
I had this need.
Basically I wanted today’s date in this format
2023-01-20T07:00:00+02:00
The requirement was T07:00:00+02:00 to always stay as-is (I didn’t want the time to change).
But I wanted today’s date to be the current date.
The quick way: toISOString()
The toISOString() method of the Date object gives you a UTC timestamp:
'2023-01-10T07:35:37.826Z'
I was reaching for the getFullYear() and all those methods to get the data out of a date, but I figured I could just cut the string returned from toISOString() so I used this:
const date = new Date().toISOString().slice(0, 10)
const value = `${date}T07:00:00+02:00`
This gives you the UTC date. That’s fine for a server running in UTC, or when UTC is what you want, but it’s the source of a classic bug.
The off-by-one-day bug
toISOString() always converts to UTC (that’s what the Z at the end means). Near midnight, the UTC date is a different day than the local one.
I’m in Italy, which is UTC+1 in winter. At 00:30 on January 1st:
const d = new Date(2026, 0, 1, 0, 30) // local time
d.toISOString().slice(0, 10) // '2025-12-31'
It’s already 2026 here, but in UTC it’s still 23:30 on December 31st. In time zones behind UTC, like New York, it happens in the evening instead: from 7pm in winter, the UTC date is already tomorrow.
Also, toISOString() throws a RangeError if the date is invalid, for example new Date('nope').toISOString(). Check user input before calling it.
The local date
If you need the user’s local calendar date, build it from the local getters:
function localDate(date = new 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}`
}
localDate(new Date(2026, 0, 1, 0, 30)) // '2026-01-01'
Two details to get right:
getMonth()starts from 0 for January, so add 1.getDate()is the day of the month.getDay()is the day of the week (0 is Sunday), a common mix-up.
padStart(2, '0') turns 1 into 01, so the string sorts and parses correctly.
The date in a specific time zone
On a server, “local” is the server’s time zone, which is often UTC. If you need the date for a given place, say the user’s time zone, use Intl.DateTimeFormat with the timeZone option and formatToParts():
function dateInTimeZone(date, timeZone) {
const parts = new Intl.DateTimeFormat('en-US', {
timeZone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).formatToParts(date)
const get = (type) => parts.find((part) => part.type === type).value
return `${get('year')}-${get('month')}-${get('day')}`
}
dateInTimeZone(new Date(), 'Europe/Rome')
dateInTimeZone(new Date(), 'America/Los_Angeles')
formatToParts() returns an array of { type, value } objects, so you pick the year, month and day by type instead of relying on the order a locale puts them in. It works in all modern browsers and in Node.js.
With Temporal
The new Temporal API has this built in. Temporal.Now.plainDateISO() returns today’s date, and its toString() is already in YYYY-MM-DD format:
Temporal.Now.plainDateISO().toString() // today in the system time zone
Temporal.Now.plainDateISO('Asia/Tokyo').toString() // today in Tokyo
Temporal isn’t available in every browser and runtime yet, so check before you rely on it. I wrote more about it in how to use the Temporal API.
Don’t hardcode the offset
My original code appends a fixed +02:00. That’s only correct half the year in Italy: in winter the offset is +01:00, because of daylight saving time.
You can build the real offset from getTimezoneOffset(). Careful with the sign: it returns minutes, and it’s positive when you’re behind UTC. For UTC+1 it returns -60.
function offsetString(date) {
const minutes = -date.getTimezoneOffset()
const sign = minutes >= 0 ? '+' : '-'
const abs = Math.abs(minutes)
const hours = String(Math.floor(abs / 60)).padStart(2, '0')
const mins = String(abs % 60).padStart(2, '0')
return `${sign}${hours}:${mins}`
}
const now = new Date()
const at7 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 7, 0)
const value = `${localDate(at7)}T07:00:00${offsetString(at7)}`
// '2026-11-27T07:00:00+01:00' in Italy in November
I compute the offset for 07:00 on that day, not for the current time, because daylight saving time changes in the middle of the night. This works for half-hour time zones too, like India’s +05:30.
This gives you the offset of the machine running the code. For a named time zone that isn’t the local one, use Temporal or a time zone library.
Parsing YYYY-MM-DD strings
Going the other way has a trap too. A date-only string is parsed as UTC midnight, while a date-time string without an offset is parsed as local time:
new Date('2023-01-20') // midnight UTC
new Date('2023-01-20T00:00:00') // midnight local time
In New York, new Date('2023-01-20').getDate() returns 19, because UTC midnight is still the evening of the 19th there. If you parse a date-only string, read it back with the UTC getters (getUTCDate(), getUTCMonth(), getUTCFullYear()), or split the string and pass the numbers to new Date(year, month - 1, day).
Want me to talk about your product? You can sponsor this site.
Related posts about js: