Skip to content

toLocaleDateString() with option { hour: '2-digit' } returns single digit hour #30369

@ameerahm

Description

@ameerahm
  • Version: v10.16.0
  • Platform: MacOS v 10.15 Darwin Kernel Version 19.0.0 RELEASE_X86_64 x86_64
  • Subsystem: date?

If the passed in hour is single digit, i.e
1am -> 9am
1pm -> 9pm

The formatted hour is single digit, even though the hour option is set to a '2-digit' format.

formatDate("11/11/2019 09:01:00");
formatDate("11/11/2019 18:05:00");

function formatDate(dateString) {
    console.log("Date string:\t", dateString);
    const options = {
        weekday: 'long',
        year: 'numeric',
        month: 'long',
        day: 'numeric',
        hour: '2-digit',
        minute: '2-digit'
    };
    const fdate = new Date(dateString);
    const fdateString = fdate.toLocaleDateString("en-US", options)
    console.log("Formatted:\t", fdateString, "\n")
}

The above code prints:

Date string: 11/11/2019 09:01:00
Formatted: Monday, November 11, 2019, 9:01 AM

Date string: 11/11/2019 18:05:00
Formatted: Monday, November 11, 2019, 6:05 PM

The formatting should return a '2-digit' hour:

Formatted: Monday, November 11, 2019, 09:01 AM
Formatted: Monday, November 11, 2019, 06:05 PM

Happy to help out, if you point me in the right direction. Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    i18n-apiIssues and PRs related to the i18n implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions