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

UTC timestamp #34

Open
username14415 opened this issue Dec 28, 2024 · 4 comments
Open

UTC timestamp #34

username14415 opened this issue Dec 28, 2024 · 4 comments

Comments

@username14415
Copy link

My example timestamp: 2024-12-28 18:32:12

Code:
const utcDate = new TZDate(timestamp, "UTC");

However, it returns: Sat Dec 28 2024 17:32:12 GMT+0000 (Coordinated Universal Time)

Why not: Sat Dec 28 2024 18:32:12 GMT+0000 (Coordinated Universal Time) ?

@jawnothin
Copy link

Same issue as this #36

The issue here is because no timezone is specified it's picking up the local devices timezone, and then converting that timestamp to UTC.

@cbejensen
Copy link

As @jawnothin said, it's using local time. To prevent that, just specify a zone/offset in your timestamp (add a Z for UTC):

2024-12-28 18:32:12Z

@username14415
Copy link
Author

Thanks, it works

  const formatMessageTimestampTZ = ({ date, timezone }) => {
    const utcTimestamp = `${date}Z`;
    const utcDate = new TZDate(utcTimestamp, "UTC");
    const tzDate = utcDate.withTimeZone(timezone);

    return tzDate;
  };

Why does it convert to the local device timezone? Wouldn't it be better to convert it to UTC by default?

@cbejensen
Copy link

I assume they were trying to keep some consistency with existing parsing norms. JS native Date assumes local time too if no zone is specified.

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

3 participants