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

ISO-8601 With offset displaying local time #40

Closed
bringking opened this issue Jan 23, 2020 · 2 comments
Closed

ISO-8601 With offset displaying local time #40

bringking opened this issue Jan 23, 2020 · 2 comments

Comments

@bringking
Copy link

I have been struggling with an ISO 8601 date string that we need to display and perserve the offset (not display it in the users local time). Here is the input in ISO-8601 format-

Source
2020-01-09T20:00-05:00

Formatting

import { toDate, format } from 'date-fns-tz';
const dateWithTz = toDate('2020-01-09T20:00-05:00');
const formatted = format(dateWithTz, ' hh:mm a - eeee, MMMM d, yyyy');

result
06:00 PM - Thursday, January 9, 2020 // my local time

expected
08:00 PM - Thursday, January 9, 2020 // the correct time in the specified offset

Am I missing a step here?

@marnusw
Copy link
Owner

marnusw commented Jan 24, 2020

It's an interesting one. These are the steps currently required:

  1. toDate parses the string taking the offset into account and giving the correct timestamp, i.e. the UTC time you're looking for.
  2. Use utcToZoneTime(dateWithTz, timeZone) to effectively indicate for which time zone you want to format.
  3. format(dateWithTz, ' hh:mm a - eeee, MMMM d, yyyy') to display for that time zone.

Whenever I've used this / thought about it the time zone to display for was available as an IANA time zone string. In your case it's not, because it's contained in the original date string.

I think utcToZoneTime might work with offsets as well as IANA time zones, so you could try slicing it off the date in order to call this (can't quite remember and didn't check the code now). Granted it's a bit messy though.

I won't have time to think about this right now, but if you can figure it out and if necessary propose a PR I'll find time to consider it.

@bringking
Copy link
Author

@marnusw so using the offset worked as intended, but I had to extract it from the ISO String like so

import { toDate, format, utcToZonedTime } from 'date-fns-tz';

const isoDateString = '2020-01-09T20:00-05:00';
const dateWithTz = toDate(isoDateString);
const [offset] = isoDateString.match(/(-|\+)([0-9][0-9]):([0-9][0-9])$/);
const timeZonedDate = utcToZonedTime(dateWithTz, offset);
format(timeZonedStartDate, 'eeee, MMMM d, yyyy', { timeZone: offset })

I will close this since it's technically possible, but have to jump through hoops. If there is a better way, would be interested to know it.

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

2 participants