【问题标题】:From string to date setting format从字符串到日期设置格式
【发布时间】:2019-12-17 22:59:40
【问题描述】:

当我尝试new Date("23/06/2019") 时,我有一个格式为“DD/MM/YYYY”的日期,我收到以下错误:

“无效日期”

我该如何解决?

【问题讨论】:

标签: angular date format date-formatting


【解决方案1】:

你传入的字符串不是 Date 'constructor' 知道如何处理的东西。 看看如何用javascript创建一个日期对象(这里是SO post)。

你现在可以试试这个作为一个快速修复:

let yourDate = "23/06/2019".split('/');
new Date(yourDate[2], yourDate[1], yourDate[0]);

let yourDate = "23/06/2019".split('/').reverse().join('-');
new Date(yourDate);

【讨论】:

  • 更正您的第一个解决方案,新日期(年,月,日),月份为 0 到 1 月,1 为 2 月...
【解决方案2】:

我建议使用 moment.js 库进行日期解析。您可以轻松解析日期字符串。

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

时刻(“23/09/2019”,“DD-MM-YYYY”)

【讨论】:

    猜你喜欢
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多