如何将字符串日期格式`October 18th 2019`转换为有效日期`2019-10-17T23:00:00.000Z`

时间:2019-11-11 12:57:56

标签: date format momentjs

我需要将类似l1 = [1,1,12] l2 = [1,1,1] for elem in l1: if not elem in l2: print(elem) print(l1.index(elem)) ('MMMM Do YYYY')的字符串日期格式转换为有效日期October 18th 2019或类似的2019-10-17T23:00:00.000Z

我尝试使用将字符串解析为一刻,但我不断收到错误消息

更新:我使用了17/10/2019。收到无效日期作为错误消息,很抱歉,我需要澄清,我正在尝试将字符串moment('October 18th 2019').format()转换为有效的日期格式,

1 个答案:

答案 0 :(得分:2)

在构造MMMM Do YYYY对象时,只需使用以下方法之一提供输入(Moment)的格式字符串:

// this way interprets the input at the start of the day in the local time zone
moment('October 18th 2019', 'MMMM Do YYYY')

// this way interprets the input at the start of the day in UTC
moment.utc('October 18th 2019', 'MMMM Do YYYY')

// this way interprets the input at the start of the day in a specific named time zone
// (requires the moment-timezone add-on)
moment.tz('October 18th 2019', 'MMMM Do YYYY', 'Europe/London')

然后,您可以根据需要对其进行格式化和/或转换。例如:

// this way keeps the local time, includes the local time offset when formatting
moment('October 18th 2019', 'MMMM Do YYYY').format()

// this way converts from local to utc before formatting
moment('October 18th 2019', 'MMMM Do YYYY').utc().format()

// this way converts from local to utc before formatting and includes milliseconds
moment('October 18th 2019', 'MMMM Do YYYY').toISOString()