date-fns / tz

date-fns timezone utils
MIT License
80 stars 5 forks source link

Unable to find documentation about local to UTC conversion #19

Closed n-ae closed 1 month ago

n-ae commented 1 month ago

How can I implement below:

import { zonedTimeToUtc } from 'date-fns-tz';

const datetimeString = '2024-10-03T08:58:50';
const timeZone = 'America/New_York';

const utcDate = zonedTimeToUtc(datetimeString, timeZone);
console.log(utcDate.toISOString()); // Outputs the date in ISO 8601 format in UTC

with current versions of date-fns libraries? I am trying to get iso formatted utc representation of a local timestamp.

camsteffen commented 1 month ago
parseISO('2024-10-03T08:58:50', { in: tz('America/New_York') }).toISOString()
n-ae commented 1 month ago

Thanks but that returns:

2024-10-03T08:58:50.000-04:00

whereas I want:

2024-10-03T12:58:50.000Z
n-ae commented 1 month ago

This is how I done it.

const datetimeString = '2024-10-03T08:58:50';
const timeZone = 'America/New_York'
const timestamp = parseISO(datetimeString, { in: tz(timeZone) })
const formattedTimestamp = formatISO(timestamp, { in: tz('UTC') })