ALSTRA
ALSTRA

Reputation: 661

Cant fromat String to Date

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

Upvotes: 0

Views: 65

Answers (2)

Arkej
Arkej

Reputation: 2251

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

Upvotes: 1

Mantas
Mantas

Reputation: 89

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

Upvotes: 0

Related Questions