在React前端的特定时区开始活动

发布于 2025-01-18 17:22:33 字数 1082 浏览 0 评论 0原文

我正在开发 React,我必须为在特定时区的精确时间(例如 2022/04/20 20:00:00 巴黎时区)开始的事件进行倒计时。

日期存储在前面的常量中,如下所示:

event:{
start: new Date('2022-04-20T20:00:00'),
end: new Date('2022-04-23T00:00:00')
}

因为日期存储在前面,所以它将对应于用户本地时区。
我不知道如何使日期保留在巴黎时区,无论用户时区是什么。
日期必须保留在日期类型中,因为我在计时器中进行倒计时计算。 我尝试使用 fns-tz 包中的 formatInTimeZone(date, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzz') 但当我在末尾添加“zzz”时,它返回无效日期格式。
另外,我注意到移动浏览器不支持某些日期格式(因此倒计时未在移动设备上显示)。
如果我更改“2022-04-20T19:00:00Z”的日期,这会是工作吗?
我知道 Z 代表 UTC,但在这种情况下它是 UTC+0 吗?

我真的对时区感到不舒服,任何帮助将不胜感激。

我尝试过:

import { formatInTimeZone } from "date-fns-tz";

const start = new Date("2022-04-20T20:00:00");
const end = new Date("2022-04-23T00:00:00");

const startdate = formatInTimeZone(start,"Europe/Paris","yyyy-MM-dd HH:mm:ss");
const enddate = formatInTimeZone(end,"Europe/Paris","yyyy-MM-dd HH:mm:ss zzz");


export const dropDate = {
    start: new Date(startdate),
    end: new Date(enddate),
};

它似乎可以工作,但不能在移动浏览器上工作。 日期格式无效。

I'm working on React and I have to do a countdown for an event that start at a precised time at a specific timezone (say 2022/04/20 20:00:00 Paris timezone).

The date is stored in a constant in the front like so :

event:{
start: new Date('2022-04-20T20:00:00'),
end: new Date('2022-04-23T00:00:00')
}

Because the date is stored in front, it will correspond to the user local timezone.
I don't know how to do so that the date stays in paris timezone, whatever the user timezone is.
The date has to stay in a Date type because I do calculations in my Timer for the countdown.
I tried to use formatInTimeZone(date, 'Europe/Paris', 'yyyy-MM-dd HH:mm:ss zzz') from the fns-tz package but when I add the "zzz" at the end it returns an invalid date format.
Also, I noticed that some Dates format are not supported on mobile browsers (the countdown did not display on mobile because of that).
if I changed the date for '2022-04-20T19:00:00Z' would it be the job ?
I know that the Z stands for UTC, but in this case is it UTC+0 ?

I'm really not confortable with the timezones and any help would be appreciated.

I tried :

import { formatInTimeZone } from "date-fns-tz";

const start = new Date("2022-04-20T20:00:00");
const end = new Date("2022-04-23T00:00:00");

const startdate = formatInTimeZone(start,"Europe/Paris","yyyy-MM-dd HH:mm:ss");
const enddate = formatInTimeZone(end,"Europe/Paris","yyyy-MM-dd HH:mm:ss zzz");


export const dropDate = {
    start: new Date(startdate),
    end: new Date(enddate),
};

It seems to work but not on mobile browser.
The date format is not valid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2025-01-25 17:22:33

如果将日期更改为'2022-04-20T19:00:00Z',它将仅考虑GMT差异。就我而言,我的GMT是+0530(IST)。因此,它只会将30分钟的30分钟增加到19:00:00,从而导致4月21日的00:30:00。
您可以使用 dayjs 库库。

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import relativeTime from "dayjs/plugin/relativeTime";
import timezonePlugin from "dayjs/plugin/timezone";

dayjs.extend(utc);
dayjs.extend(relativeTime);
dayjs.extend(timezonePlugin);

event:{
    start: dayjs("2022-04-20T23:30:00").tz('Europe/Paris'),//This will set the start time to 2022/04/20 20:00:00 Paris timezone
    end: dayjs("2022-04-23T03:30:00").tz('Europe/Paris') // This will set the end time to 2022/04/23 00:00:00 Paris timezone.
}

If you change the date to '2022-04-20T19:00:00Z', it will just factor in the GMT difference. In my case, My GMT is +0530(IST). So, it will just add 5hrs 30 mins to 19:00:00 which results in 00:30:00 of 21st April.
You can make use of dayjs library instead.

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import relativeTime from "dayjs/plugin/relativeTime";
import timezonePlugin from "dayjs/plugin/timezone";

dayjs.extend(utc);
dayjs.extend(relativeTime);
dayjs.extend(timezonePlugin);

event:{
    start: dayjs("2022-04-20T23:30:00").tz('Europe/Paris'),//This will set the start time to 2022/04/20 20:00:00 Paris timezone
    end: dayjs("2022-04-23T03:30:00").tz('Europe/Paris') // This will set the end time to 2022/04/23 00:00:00 Paris timezone.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文