aws-amplify / amplify-codegen-ui

Generate React components for use in an AWS Amplify project.
Apache License 2.0
32 stars 26 forks source link

`convertToLocal` incorrectly parses dates between midnight and 1am #937

Open cwoolum opened 1 year ago

cwoolum commented 1 year ago

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 commented 1 year ago

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