Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,51 @@ const options = {
day: "numeric",
};
console.log(date.toLocaleString("de-DE", options));
// "Donnerstag, 20. Dezember 2012"
// Example output: "Donnerstag, 20. Dezember 2012"
// The exact date may shift depending on your local time zone.

// An application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
console.log(date.toLocaleString("en-US", options));
// "Thursday, December 20, 2012, GMT"
// Example output: "Thursday, December 20, 2012 at UTC"

// Sometimes even the US needs 24-hour time
console.log(date.toLocaleString("en-US", { hour12: false }));
// "12/19/2012, 19:00:00"
// Example output: "12/19/2012, 19:00:00"
// The exact date and time may shift depending on your local time zone.
```

### Controlling single-digit vs double-digit day or month

Use `numeric` for single digits and `2-digit` for leading zeros.
The output may still follow the locale's style.
For example, `en-GB` often pads even with `numeric`.

```js
const date = new Date(Date.UTC(2012, 1, 2, 3, 0, 0));

// Single-digit day and month
const options = {
timeZone: "UTC",
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
};
console.log(date.toLocaleString("en-US", options));
// Example output: "2/2/2012, 3:00 AM"
// The exact date and time may shift depending on your local time zone.

// Double-digit day and month
options.month = "2-digit";
options.day = "2-digit";
options.hour = "2-digit";
options.minute = "2-digit";
console.log(date.toLocaleString("en-US", options));
// Example output: "02/02/2012, 03:00 AM"
// The exact date and time may shift depending on your local time zone.
```

## Specifications
Expand Down