【发布时间】:2021-04-21 02:51:56
【问题描述】:
我有一个节点 + 打字稿项目。并且正在执行以下操作:
export class Slot {
startTime: Date;
constructor(_startTime: Date){
this.startTime = _startTime
}
}
// In some controller method
const slot = new Slot(new Date()); // Here the date is in the System Timzone
// Some logic for the Date object before sending the response
response.json(slot);
现在,在响应中,startTime 没有保留系统 TZ,它被转换为 UTC。 要求在系统时区发送它。我可以手动执行 .toString() 这将使其进入系统时区,但要避免它,因为 Date 对象正在处理很多在响应中发送之前放置。
【问题讨论】:
-
嗨。我建议检查您对
Date对象本身的理解。它不保留任何时区。它只是一个数字的包装器——一个以毫秒为单位的 Unix 时间戳,它本质上是基于 UTC 的。因此,您的评论“这里的日期在系统时区中”是不正确的。
标签: node.js typescript express date timezone