Cant fromat String to Date

纵然是瞬间 提交于 2019-12-25 15:16:33

问题


When I try to convert a string value to a Date I get the error message "Invalid date"

timestamp : string = "2017:03:22 08:45:22";
.
.
let time = new Date(timestamp);
console.log("Time: ",time); //here I get   Time: Invalid date

回答1:


Since your string must be in ISO date format you can change it like in the code below:

let timestamp : string = "2017:03:22 08:45:22";

let timestampISO : string = timestamp.replace(':','-').replace(':','-').replace(' ','T');

let time = new Date(timestampISO);

console.log("Time: ",time);



回答2:


Your date must be a version of an ISO format.

To be more specific, it must be a version of ISO8601. See more here.

Example:

let time = new Date("2017/03/22 08:45:22");


来源:https://stackoverflow.com/questions/42947680/cant-fromat-string-to-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!