Skip to content

regression - toLocaleString as UTC TZ is displayed as short date (1 digit) on Node 21 but (2 digits) on Node 18 #51090

Closed
@ghiscoding

Description

@ghiscoding

Version

21.4.0

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

No response

What steps will reproduce the bug?

Running the following script with today's date (2023-12-07) is displayed differently on Node 21 than it is on Node 18, this seems like a regression and I caused some failing unit tests in my CI because it expected a date format of YYYY-MM-DD but that is no longer true in Node 21.

This simple script

const formatted = new Date().toLocaleString('sv-SE', {
  timeZone: 'UTC'
});
    
console.log(formatted);

is displaying the following

Node 21

notice the December 7th is displayed as 7 instead of the expected 07

2023-12-7
Node 18
2023-12-07

How often does it reproduce? Is there a required condition?

every time I tried on Node 21.4.x

What is the expected behavior? Why is that the expected behavior?

It should provide an ISO formatted date

What do you see instead?

December 7th is displayed as 7 instead of 07, below is a print screen of the simple script that I ran on Node 18 and 21

image

Additional information

This date format script is used by conventional-changelog library here and the unit tests I have in Lerna-Lite failed only on Node 21 in this GitHub Action CI workflow because it expected a format of YYYY-MM-DD but it instead provided an unexpected format of YYYY-MM-D.

The reason my unit tests failed is because, the tests are using a serializer replace any date to a static 'YYYY-MM-DD' text that the tests can compare test snapshot, but it started failing on Node 21 because the regex no longer found the expected date format. For now I changed the regex from formattedDate.replace(/\(\d{4}-\d{2}-\d{2}\)/g, '(YYYY-MM-DD)') to formattedDate.replace(/\(\d{4}-\d{1,2}-\d{1,2}\)/g, '(YYYY-MM-DD)') and my tests are now passing again but it's still seem to be regression in Node 21

Note that I'm on Windows but my CI is running on Ubuntu, so it's not an OS specific issue

As for the use of sv-SE locale, it looks like it's because Sweden has a fixed format that is unlikely to change and is the nearest to the ISO format as explained in this Stack Overflow answer

Metadata

Metadata

Assignees

Labels

i18n-apiIssues and PRs related to the i18n implementation.icuIssues and PRs related to the ICU dependency.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions