Moment.js 获取 UTC 时间作为日期对象而不是字符串
Moment.js to get UTC time as date object instead of string
我想使用 moment.utc()
作为日期对象而不是纪元或字符串(moment.utc().format()
或使用 .toISOString()
)来获取 UTC 时间。 moment.utc().toDate()
returns 我的当地时间。任何帮助将不胜感激。
您可以使用 moment().toDate()
。如果将日期强制转换为字符串(例如,通过将其发送到控制台、警报等),内置(依赖于实现)toString 方法通常会使用主机时区设置生成一个字符串,例如
var m = moment().toDate(); // Equivalent to new Date()
console.log(m + '') // coerce to string uses built-in toString
console.log(m.toISOString()) // ISO 8601 string offset +0000
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
我想使用 moment.utc()
作为日期对象而不是纪元或字符串(moment.utc().format()
或使用 .toISOString()
)来获取 UTC 时间。 moment.utc().toDate()
returns 我的当地时间。任何帮助将不胜感激。
您可以使用 moment().toDate()
。如果将日期强制转换为字符串(例如,通过将其发送到控制台、警报等),内置(依赖于实现)toString 方法通常会使用主机时区设置生成一个字符串,例如
var m = moment().toDate(); // Equivalent to new Date()
console.log(m + '') // coerce to string uses built-in toString
console.log(m.toISOString()) // ISO 8601 string offset +0000
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>