如何将UTC DateTime转换为本地DateTime,而不会以不同的格式转换

发布于 2025-01-28 06:23:23 字数 718 浏览 2 评论 0原文

我有这种格式的特定日期时间值' yyyy-mm-ddthh:mm:ss.ms '(eg' 2022-05-10T13:44:00.0000000 ')我需要将它们转换为本地DateTime(对我来说是UTC+2),而不会更改格式(So' 2022-05-10T15:44:00.0000000 '是所需的结果(甚至更好的结果是更好没有毫秒,但这只是锦上添花)。

我已经搜索了很广泛的搜索,但是我发现的每一个所谓的解决方案都会更改格式,或者根本不会改变时间。

这就是我现在拥有的,它成功地将时间转换为本地时间,但是通过.toisostring()将原始格式运行以使其转换回UTC时间。

//Input: event.start.dateTime = '2022-05-10T13:44:00.0000000'

let startDateTime = new Date(event.start.dateTime);
startDateTime.setMinutes(startDateTime.getMinutes() - 
startDateTime.getTimezoneOffset());
document.getElementById('ev-start').value = 
startDateTime.toISOString().slice(0,16);

//Output: '2022-05-10T13:44:00.000Z'

I have specific DateTime values in this format 'YYYY-MM-DDThh:mm:ss.ms' (e.g. '2022-05-10T13:44:00.0000000') and I need to convert them to local DateTime (which is UTC+2 for me) without changing the format (so '2022-05-10T15:44:00.0000000' is the desired result (even better would be without the milliseconds but that would just be the icing on the cake)).

I've searched far and wide but every alleged solution I find either changes the format or doesn't change the time at all.

This is what I have right now, it successfully converts the time to local time but by running it through .toISOString() to get the original format back it converts it back to UTC time.

//Input: event.start.dateTime = '2022-05-10T13:44:00.0000000'

let startDateTime = new Date(event.start.dateTime);
startDateTime.setMinutes(startDateTime.getMinutes() - 
startDateTime.getTimezoneOffset());
document.getElementById('ev-start').value = 
startDateTime.toISOString().slice(0,16);

//Output: '2022-05-10T13:44:00.000Z'

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

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

发布评论

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

评论(2

沉鱼一梦 2025-02-04 06:23:24

我找不到一个干净且令人满意的解决方案,所以我决定自己的格式。这是我最终得到的:

输入:' 2022-05-10T13:44:00.0000000 '

let startDateTime = new Date(event.start.dateTime);
startDateTime.setMinutes(startDateTime.getMinutes() - 
startDateTime.getTimezoneOffset());
let localStartDateTime = startDateTime.getFullYear() + "-" + 
       (startDateTime.getMonth() + 1).toString().padStart(2, '0') +
       "-" + startDateTime.getDate().toString().padStart(2, '0') + 
       "T" + startDateTime.getHours().toString().padStart(2, '0') + 
       ":" + startDateTime.getMinutes().toString().padStart(2, '0') 
       + ":" +
       startDateTime.getSeconds().toString().padStart(2, '0');
       document.getElementById('ev-start').value = 
       localStartDateTime;

输出:' 2022-05-10T15:44:00 ''

I couldn't find a clean and satisfying solution so I decided to just to the formatting myself. Here's what I ended up with:

Input: '2022-05-10T13:44:00.0000000'

let startDateTime = new Date(event.start.dateTime);
startDateTime.setMinutes(startDateTime.getMinutes() - 
startDateTime.getTimezoneOffset());
let localStartDateTime = startDateTime.getFullYear() + "-" + 
       (startDateTime.getMonth() + 1).toString().padStart(2, '0') +
       "-" + startDateTime.getDate().toString().padStart(2, '0') + 
       "T" + startDateTime.getHours().toString().padStart(2, '0') + 
       ":" + startDateTime.getMinutes().toString().padStart(2, '0') 
       + ":" +
       startDateTime.getSeconds().toString().padStart(2, '0');
       document.getElementById('ev-start').value = 
       localStartDateTime;

Output: '2022-05-10T15:44:00'

欢你一世 2025-02-04 06:23:24

希望这会有所帮助

const startDateTime = new Date('2022-05-10T13:44:00.0000000');

const outputDateTime = new Date(startDateTime.toString()).toISOString().slice(0, 19);

//document.getElementById('ev-start').value = outputDateTime

console.log(outputDateTime);

Hope this helps

const startDateTime = new Date('2022-05-10T13:44:00.0000000');

const outputDateTime = new Date(startDateTime.toString()).toISOString().slice(0, 19);

//document.getElementById('ev-start').value = outputDateTime

console.log(outputDateTime);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文