gpt4 book ai didi

javascript - 如何返回按特定偏移量移动的小时 : minute string,?

转载 作者:行者123 更新时间:2023-12-02 22:32:55 26 4
gpt4 key购买 nike

所以我有一个日期字符串:

const date = '2000-01-01T01:00:00Z';

和偏移量(UTC 给定日期和我的时区之间的分钟差):

const offset = 60;

我想实现这个:

02:00

因此,我需要以某种方式将日期中的时间移动特定的分钟数(偏移量),并仅返回 HH:mm 格式的时间。

我尝试使用 moment.js 和纯 JS Date 对象,但问题是,当我将 date 放入其构造对象中时,它们会立即考虑我的本地浏览器时间(我希望省略)。我该怎么办?

最佳答案

Moment js 文档(解析)- https://momentjs.com/docs/#/parsing/ :

moment.utc(...) is utc mode. Ambiguous input is assumed to be UTC. Unambiguous input is adjusted to UTC.

应以 UTC 时间解析您的日期

然后使用函数add - https://momentjs.com/docs/#/manipulating/

添加您的分钟数然后格式化-https://momentjs.com/docs/#/displaying/

moment.utc('2000-01-01T01:00:00Z').add(60, 'm').format('h:mm')

关于javascript - 如何返回按特定偏移量移动的小时 : minute string,?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58837462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com