marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chrome 67 - 72: Time incorrectly parsed

Semigradsky opened this issue · comments

For Chrome 67 currently 18:00 is parsed as 6:00.

See

// New browsers use `hourCycle`, IE and Chrome <73 does not support it and uses `hour12`
var testDateFormatted = new Intl.DateTimeFormat('en-US', {
hour12: false,
timeZone: 'America/New_York',
year: 'numeric',
month: 'numeric',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
}).format(new Date('2014-06-25T04:00:00.123Z'))
var hourCycleSupported =
testDateFormatted === '06/25/2014, 00:00:00' ||
testDateFormatted === '‎06‎/‎25‎/‎2014‎ ‎00‎:‎00‎:‎00'
dtfCache[timeZone] = hourCycleSupported
? new Intl.DateTimeFormat('en-US', {
hour12: false,
timeZone: timeZone,
year: 'numeric',
month: 'numeric',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
: new Intl.DateTimeFormat('en-US', {
hourCycle: 'h23',
timeZone: timeZone,
year: 'numeric',
month: 'numeric',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})

We have comment here:

New browsers use hourCycle, IE and Chrome <73 does not support it and uses hour12

But not hourCycle tested and in code hour12: false used if the hour cycle is supported. It looks like checking should be changed and if branches should be swapped.

Example (in Chrome 67):

const timeZone = 'Europe/Riga'
const date = new Date('2023-04-04T18:00:00.000Z')

new Intl.DateTimeFormat('en-US', {
  hourCycle: 'h23',
  timeZone: timeZone,
  year: 'numeric',
  month: 'numeric',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
}).formatToParts(date)
/*
0: {type: "month", value: "4"}
1: {type: "literal", value: "/"}
2: {type: "day", value: "04"}
3: {type: "literal", value: "/"}
4: {type: "year", value: "2023"}
5: {type: "literal", value: ", "}
6: {type: "hour", value: "9"} // <- hour will be parsed as `09`
7: {type: "literal", value: ":"}
8: {type: "minute", value: "00"}
9: {type: "literal", value: ":"}
10: {type: "second", value: "00"}
11: {type: "literal", value: " "}
12: {type: "dayperiod", value: "PM"}
*/

new Intl.DateTimeFormat('en-US', {
  hour12: false,
  timeZone: timeZone,
  year: 'numeric',
  month: 'numeric',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
}).formatToParts(date)
/*
0: {type: "month", value: "4"}
1: {type: "literal", value: "/"}
2: {type: "day", value: "04"}
3: {type: "literal", value: "/"}
4: {type: "year", value: "2023"}
5: {type: "literal", value: ", "}
6: {type: "hour", value: "21"} // <- hour is okay
7: {type: "literal", value: ":"}
8: {type: "minute", value: "00"}
9: {type: "literal", value: ":"}
10: {type: "second", value: "00"}
*/

@marnusw Could you please take a look?

UPD:
Affected browsers: at least Chrome 67 - 72

Any updates on this?