如何将UTC DateTime转换为本地DateTime,而不会以不同的格式转换
我有这种格式的特定日期时间值' 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找不到一个干净且令人满意的解决方案,所以我决定自己的格式。这是我最终得到的:
输入:' 2022-05-10T13:44:00.0000000 '
输出:' 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'
Output: '2022-05-10T15:44:00'
希望这会有所帮助
Hope this helps