Find Related products on Amazon

Shop on Amazon

Why are 2025/05/28 and 2025-05-28 different days in JavaScript?

Published on: 2025-06-18 18:09:31

Why are 2025/05/28 and 2025-05-28 different days in JavaScript? While setting up this site itself, I ran into the following oddity: console . log ( new Date ( '2025/05/28' ). toDateString ()); console . log ( new Date ( '2025-05-28' ). toDateString ()); console . log ( new Date ( '2025-5-28' ). toDateString ()); You may get different results on your machine! What's going on? A Date in JavaScript always represents a point in time (i.e. milliseconds since epoch). This is more apparent when printing out the full date string: const date = new Date ( '2025/05/28' ); console . log (date); console . log (date. toDateString ()); In this case, the passed-in date string is being interpreted as a timestamp in my local time zone. toDateString() also operates relative to the local time and so we get the same day-of-the-month back out. The difference with '2025-05-28' is in parsing behavior; the string is interpreted as UTC and so ends up at a different point in time: const date = new Date ( '20 ... Read full article.