如何使用 Javascript 日期时区显示少一天
该应用程序位于葡萄牙,其时区为 UTC+00:00,但现在是夏令时。
这里我从Datepicker组件收到了一系列日期值,即'01/04/2022 - 01/05/2022'并以
const handleCreationDateChange = (dateRange: {
startDate: Date;
endDate: Date;
}) => {
if (dateRange) {
setCreationDate(dateRange);
} else {
setCreationDate(null);
}
};
此时的状态保存,日期范围对象是正确的,如下图
const payload = {
creationDateFrom: dateRange.creationDateFrom,
creationDateTo: dateRange.creationDateTo
}
< a href="https://i.sstatic.net/7QJ9i.png" rel="nofollow noreferrer">
我尝试转换为 .toIsoString()
然后转换回日期,但它不起作用,
这需要为来自不同时区的用户工作
The application is based in Portugal whose timezone is UTC+00:00 but now is daylight saving.
Here I received a range of date values from the Datepicker component, which was '01/04/2022 - 01/05/2022' and saved it in a state
const handleCreationDateChange = (dateRange: {
startDate: Date;
endDate: Date;
}) => {
if (dateRange) {
setCreationDate(dateRange);
} else {
setCreationDate(null);
}
};
at this point, the date range object is correct, like the image below
const payload = {
creationDateFrom: dateRange.creationDateFrom,
creationDateTo: dateRange.creationDateTo
}
but the payload object which was sent to the server shows less a day
for a user in Brasil, it shows the correct date but takes a different timezone
I've tried converting to .toIsoString()
then converting back to Date but it didn't work
this needs to work for users from different timezones
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论