【发布时间】:2020-12-06 12:38:36
【问题描述】:
上下文:我在云中的虚拟机上托管了一台服务器。当只使用moment 库来初始化日期时,我总是得到一个有 +- 几个小时的日期。经过快速研究,我找到了moment-timezone 库,它应该可以解决这个问题,但我遇到了一些问题。
我有以下日期:"transactionDate": "2020-08-25 18:30:00" 已提交。
我的代码现在看起来像这样,其中 moment 现在是 moment-timezone 库:
const momentTransactionDate = moment.tz(transactionDate, "YYYY-MM-DD HH:mm:ss", "Europe/Bucharest");
const formattedTransactionDate = momentTransactionDate.format(
"YYYY-MM-DD HH:mm:ss"
);
这将输出以下值:
"momentTransactionDate": "2020-08-25T15:30:00.000Z",
"formattedTransactionDate": "2020-08-25 18:30:00",
为了测试,我只是从 Postman 向本地服务器发出请求。
为什么momentTransactionDate 是3 小时前,format() 函数为什么格式化不同的时间?
有人能解释一下这是如何工作的吗?
【问题讨论】:
标签: javascript momentjs moment-timezone