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

How to parse times in local timezone? #36

Open
showaltb opened this issue Jul 11, 2024 · 1 comment
Open

How to parse times in local timezone? #36

showaltb opened this issue Jul 11, 2024 · 1 comment

Comments

@showaltb
Copy link

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
Copy link
Author

After some digging, I see that 24hr format accepts an offset, but 12hr format apparently does not.

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