0

I'm trying to convert a date object to UTC. Either I'm using it wrong or the Date.toLocaleString seems to be broken or inadequate.

new Date('Tue Aug 09 2022 18:43:00 GMT-0500 (Central Daylight Time)')
    .toLocaleString('en-US', {
      timeZone: "UTC",
      day: "2-digit",
      hour12: false,
      year: "numeric",
      month: "2-digit",
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
    })

output:

"08/09/2022, 23:43:00" (A valid date/time)

new Date('Tue Aug 09 2022 19:43:00 GMT-0500 (Central Daylight Time)')
    .toLocaleString('en-US', {
      timeZone: "UTC",
      day: "2-digit",
      hour12: false,
      year: "numeric",
      month: "2-digit",
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit',
    })

output:

"08/10/2022, 24:43:00"

I expected it to be "08/10/2022, 00:43:00" but instead it appears to not reset the hours.

I have worked around this, in the particular way I was using the result, but I would like to be able to go without the extra conditional and reset.

    var parts = str.match(/(\d{2})\/(\d{2})\/(\d{4}),\s(\d{2})\:(\d{2})\:(\d{2})/),
      month = parseInt(parts[1], 10),
      day = parseInt(parts[2], 10),
      year = parseInt(parts[3], 10),
      hours = parseInt(parts[4], 10),
      minutes = parseInt(parts[5], 10),
      seconds = parseInt(parts[6], 10);

    if (hours == 24) {
      hours = 0;
    }

Why is this happening, and how can I accomplish my goal?

fartwhif
  • 321
  • 3
  • 12

2 Answers2

2

Use hourCycle: "h23" instead of hour12: false in your options. Read more about the options on MDN.

console.log(
  new Date('Tue Aug 09 2022 19:43:00 GMT-0500 (Central Daylight Time)')
  .toLocaleString('en-US', {
    timeZone: "UTC",
    day: "2-digit",
    hourCycle: "h23",
    year: "numeric",
    month: "2-digit",
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
);
.as-console-wrapper { max-height: 100% !important; height: 100%; }
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Very good. Marking this as the accepted answer. I'm using an outdated version of typescript and due to this: https://github.com/microsoft/TypeScript/issues/34399 and because I'm unable to upgrade this project's typescript I can't use the hourCycle property. – fartwhif Aug 09 '22 at 21:34
1

I suppose it's the americain way of displaying time after midnight when using 24-hour (non 12 hour) time. Try using en-UK for locale:

console.log(
  new Date('Tue Aug 09 2022 19:43:00 GMT-0500 (Central Daylight Time)')
  .toLocaleString('en-UK', {
    timeZone: "UTC",
    day: "2-digit",
    hour12: false,
    year: "numeric",
    month: "2-digit",
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  })
);
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • Thanks, this would work, I would have to reinterpret the output accordingly, but I would rather just reset the hours myself. 12 of one, one half dozen of the other, I suppose. So weird, I've never encountered that kind of nonsensical time before, to think it's "US specific" is a joke, maybe something highly specific to something associated with the US, but certainly not commonplace! – fartwhif Aug 09 '22 at 20:54
  • It *is* weird. The only thing I found on that strange notation was [here](https://editorsmanual.com/articles/time-24-hour-clock/) (bottom of the page) – KooiInc Aug 09 '22 at 20:59
  • https://www.militarytime.us/midnight-military-time/ explains it well. It can be a kind of connotation denoting end of day or start of day, It would be interesting to see a practical use of 'en-US',hour12: false – fartwhif Aug 17 '22 at 14:02