-
Notifications
You must be signed in to change notification settings - Fork 118
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
zonedTimeToUtc is not converting Utc time #174
Comments
Merits of function names aside, this is intended behaviour. When you are working with a zoned time you shouldn't be specifying that using a UTC time via an ISO string as you're doing, that defeats the purpose. Create a new date using the constructor directly, i.e. Put another way, if you have the correct UTC time to start with, why use |
@marnusw : I have many problems with understanding what this f-on does. In order to be more clear I' suggestion next changes in documentation: Current: Given a date and any time zone, returns a Date with the equivalent UTC time. Current: (picked in any time zone) |
@marnusw I'm here because of material ui's x-datetime/picker. It is enforcing editing in a user's locale. What I'm building is a scientific app and it must be in UTC. So, I start with UTC, but the picker forces locale. I'm trying to override that back to UTC using |
any update on this ?? facing the same issue:
|
I agree that this function purpose is not understandable and the name is ambiguous. I though it was something useful to convert local dates to UTC, which is useful in several use cases... Is there anything like that in date-fns? |
It looks like need to remove the latest "Z" from "zonedTime" string:
|
i'm also struggling with this, though my problem isn't exactly the same as OP i'm trying to get the local EST date object and transform it to UTC
output:
i'm pulling my hair out because of this. am i doing something wrong? |
See the open issue here: marnusw/date-fns-tz#174 UIEXT-941 (Date&Time widget displays always browser timezone)
I am pretty sure that this problem (the original problem as well as this) is related to #302. I have been working around that other issue by recreating internal methods like so import { toDate, type OptionsWithTZ } from "date-fns-tz";
// @ts-expect-error
import tzParseTimezone from "@@/node_modules/date-fns-tz/_lib/tzParseTimezone";
// @ts-expect-error
import tzPattern from "@@/node_modules/date-fns-tz/_lib/tzPattern";
export const fromZonedTime = (
date: string | Date,
timeZone: string,
options?: OptionsWithTZ,
) => {
if (typeof date === "string" && !date.match(tzPattern)) {
return toDate(
date,
Object.assign(Object.assign({}, options), { timeZone }),
);
}
date = toDate(date, options);
const offsetMilliseconds = tzParseTimezone(timeZone, date);
return new Date(date.getTime() + offsetMilliseconds);
};
export const toZonedTime = (
date: string | Date,
timeZone: string,
options?: OptionsWithTZ,
) => {
date = toDate(date, options);
const offsetMilliseconds = tzParseTimezone(timeZone, date, true);
return new Date(date.getTime() - offsetMilliseconds);
}; and with that also the problems above vanished. |
It seems like that
zonedTimeToUtc
is not converting to UTC time, instead, it's converting to zoned time into your local time.Here's an example, in which you try to convert something from your timezone to UTC. However, nothing is changed, since the zoned is the same as your local time.
So, it seems like
zonedTimeToUtc
should be called aszonedTimeToLocal
.Tested with:
The text was updated successfully, but these errors were encountered: