Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convertToLocal incorrectly parses dates between midnight and 1am #937

Open
cwoolum opened this issue Mar 6, 2023 · 1 comment
Open

convertToLocal incorrectly parses dates between midnight and 1am #937

cwoolum opened this issue Mar 6, 2023 · 1 comment

Comments

@cwoolum
Copy link
Contributor

cwoolum commented Mar 6, 2023

Given the following code

 const convertToLocal = (date) => {
    const df = new Intl.DateTimeFormat("default", {
      year: "numeric",
      month: "2-digit",
      day: "2-digit",
      hour: "2-digit",
      minute: "2-digit",
      calendar: "iso8601",
      numberingSystem: "latn",
      hour12: false,
    });
    const parts = df.formatToParts(date).reduce((acc, part) => {
      acc[part.type] = part.value;
      return acc;
    }, {});
    return `${parts.year}-${parts.month}-${parts.day}T${parts.hour}:${parts.minute}`;
  };

If I pass in a date like 2023-03-06T08:58:00.000Z, It incorrectly treats the hour as 24 instead of 0. This leads to an invalid date of 2023-03-06T24:58

@cwoolum
Copy link
Contributor Author

cwoolum commented Mar 6, 2023

The fix here might be to switch from hour12: false to hourCycle: 'h23' to display the hours from 00:00 to 23:59.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant