Moment js:仅在 UTC 中保留 UTC date/time
Moment js: Keep UTC date/time in UTC only
date = moment(startDate).startOf('day');
date.format('2019-01-01't)
以上代码将 UTC 日期转换为本地日期。我如何保持 UTC 日期 UTC?
startDate 是 iso 格式的日期时间字符串
来自moment docs:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
moment.utc()
instead of moment()
.
因此,即使您的日期字符串是 UTC 并且 moment 正确解析了日期,它仍然会以当地时间显示输出,除非您使用 moment.utc()
。要以 utc 格式显示:
const s = '2019-03-08T14:59:40Z';
const date = moment.utc(s).startOf('day').format();
console.log(date);
// 2019-03-08T00:00:00Z
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
date = moment(startDate).startOf('day');
date.format('2019-01-01't)
以上代码将 UTC 日期转换为本地日期。我如何保持 UTC 日期 UTC?
startDate 是 iso 格式的日期时间字符串
来自moment docs:
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use
moment.utc()
instead ofmoment()
.
因此,即使您的日期字符串是 UTC 并且 moment 正确解析了日期,它仍然会以当地时间显示输出,除非您使用 moment.utc()
。要以 utc 格式显示:
const s = '2019-03-08T14:59:40Z';
const date = moment.utc(s).startOf('day').format();
console.log(date);
// 2019-03-08T00:00:00Z
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>