javascript - Invalid Date error trying to parse a string containing AM or PM using npm date-and-time library in javascript
问题描述
i get invalid date error trying to parse a string containing PM or AM with time-and-date, here is my code:
const date = require('date-and-time');
let d = date.parse('Dec, 25 2019 at 3:51 AM', 'MMM, D YYYY at h:m A');
//same error with following line also
//let d = date.parse('Dec, 25 2019 at 3:51 AM', 'MMM, D YYYY at h:m AA (*)');
console.log(d);
result:
Invalid Date
i'm referencing their document here: https://www.npmjs.com/package/date-and-time/v/0.11.1
the page has following lines in the document, i wonder if I misunderstood them:
A meridiem a.m., p.m.
a (*) meridiem A.M., P.M.
AA (*) meridiem AM, PM
aa (*) meridiem am, pm
解决方案
正如@ChrisG 评论的那样,问题是您使用的是AM
/PM
而不是a.m.
/ p.m.
。
它在使用后者时有效,请参阅此沙箱。
编辑:
如果您想使用 AM/PM,您应该在该包上创建功能请求或切换到另一个包(例如,时刻)
推荐阅读
- c# - 我可以使用实体框架中的更新方法更新实体中的不可为空的值吗?
- java - Log4j2 可以同时配置 .xml 和 .properties 吗?
- reactjs - 福米克 | 错误 useFormikContext formik 值未定义
- python - Can we reset .requires_grad of all defined tensors in a code to zero at once
- arrays - Valgrind 错误 - 条件跳转或移动取决于未初始化的值
- image - Yocto:如何为图像制作可变配方?
- python - Flask:获取上传文件的修改日期
- c++ - 如何创建一个接受具有不同签名的函数指针的 C++ 类?
- swift - 如何检测 actionSheet 是否被解除?
- javascript - 在 Eclipse IDE 中使用 Ant 缩小 JavaScript 文件,错误:Missing Name After . 操作员