最佳答案
我使用Moment.js来解析和格式化我的web应用程序中的日期。作为JSON对象的一部分,我的后端服务器以UTC epoch (Unix偏移量)的毫秒数发送日期。
解析日期在特定的时区很简单——只需在解析之前将RFC 822时区标识符附加到字符串的末尾:
// response varies according to your timezone
const m1 = moment('3/11/2012 13:00').utc().format("MM/DD HH:mm")
// problem solved, always "03/11 17:00"
const m2 = moment('3/11/2012 13:00 -0400').utc().format("MM/DD HH:mm")
console.log({ m1, m2 })
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
But how do I format a date in a specifc timezone?
I want consistent results regardless of the browser's current time, but I don't want to display dates in UTC.