包含 'T' 和 'Z' 个字符的日期字符串
Date String with 'T' and 'Z' characters
我正在使用以下格式在 sql 数据库中保存日期:
Moment(new Date()).format('YYYY-MM-DD HH:mm:ss', 'true')
当我从数据库中检索相同的内容时,dateString 显示为:2020-09-15T20:05:28.000Z
;反过来,如果我做 Moment('2020-09-15T20:05:28.000Z').format('ddd MMM DD GGGG, h:mm A')
如果返回 Wed Sep 16 2020, 1:35 AM
这实际上是不正确的。
有人能解释一下吗?
从 DB 中检索的时间是 UTC 格式,您正在用本地时间解析它
根据 doc
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC
因此,如果您想按预期显示,请使用 .utc
方法
let time = moment("2020-09-15T20:05:28.000Z")
.utc()
.format("ddd MMM DD GGGG, h:mm A")
console.log(time)
<script src="https://momentjs.com/downloads/moment.min.js"></script>
我正在使用以下格式在 sql 数据库中保存日期:
Moment(new Date()).format('YYYY-MM-DD HH:mm:ss', 'true')
当我从数据库中检索相同的内容时,dateString 显示为:2020-09-15T20:05:28.000Z
;反过来,如果我做 Moment('2020-09-15T20:05:28.000Z').format('ddd MMM DD GGGG, h:mm A')
如果返回 Wed Sep 16 2020, 1:35 AM
这实际上是不正确的。
有人能解释一下吗?
从 DB 中检索的时间是 UTC 格式,您正在用本地时间解析它
根据 doc
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC
因此,如果您想按预期显示,请使用 .utc
方法
let time = moment("2020-09-15T20:05:28.000Z")
.utc()
.format("ddd MMM DD GGGG, h:mm A")
console.log(time)
<script src="https://momentjs.com/downloads/moment.min.js"></script>