不能从字符串到日期 [英] Cant fromat String to Date

查看:101
本文介绍了不能从字符串到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将字符串值转换为日期时,收到错误消息无效的日期"

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

推荐答案

由于您的字符串必须为ISO日期格式,因此可以像下面的代码中一样更改它:

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);

这篇关于不能从字符串到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆