any-date-parser icon indicating copy to clipboard operation
any-date-parser copied to clipboard

How to parse times in local timezone?

Open showaltb opened this issue 7 months ago • 1 comments

Inputs with absolute time (other than ISO8601 with time zone offset) seem to be parsed as UTC. Here is a simple NodeJS 20.12.0 example for America/New_York time zone (UTC-4 for the date used below):

import 'any-date-parser'

const input = '7/11/2024 04:15:00 PM'
console.log('input:', input)
const date = Date.fromString(input)
console.log('date:', date)
console.log('toLocaleString:', date.toLocaleString())

Outputs:

input: 7/11/2024 04:15:00 PM
date: 2024-07-11T16:15:00.000Z
toLocaleString: 7/11/2024, 12:15:00 PM.   # <-- I would like 04:15:00 PM

If the input were interpreted in local time zone, UTC time would be 20:15:00Z

I've tried appending a time zone offset to the input string, but I either get parsing errors, or the offset is interpreted as the time itself.

Is it possible to parse input times in the local time zone? So that:

Date.fromString(someDate.toLocaleString()) === someDate

Thanks!

showaltb avatar Jul 11 '24 21:07 showaltb