Moment JS:从丢失的时区字符串中获取日期和时间

Moment JS: Get the date and time from the lost timezone string

我有一些格式化的日期时间字符串,像这样 Fri Sep 18th 2020 05:52:20
当我使用 momentJS 将主题转换为日期时间时 => 失败。因为那是无效的日期时间格式。
那么,如何将它们转换为带时区的日期时间(即:'Euro/Paris')。

如果您的日期格式不是标准格式,您可以为其指定一个格式字符串作为第二个参数

(https://momentjs.com/docs/#/parsing/string-format/)

格式字符串:3 个字母的星期几、3 个字母的月份、序数日、4 位年份、带前导 0 的 24 小时时间格式是:

ddd MMM Do YYYY HH:mm:ss

您可以使用 moment().tz

指定时区
moment.tz(
    moment(
        "Fri Sep 18th 2020 05:52:20",
        "ddd MMM Do YYYY HH:mm:ss"
    ), "Europe/Paris")
    .format("YYYY-MM-DD HH:mm:ss z"); // "2020-09-18 12:52:20 CEST"

另外请记住 RobG 对时区的评论

the string is parsed as local, it's the output that is shifted to the specified timezone. So the resulting timestamp values are dependent on the host system timezone settings