0

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
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ALSTRA
  • 661
  • 3
  • 12
  • 30
  • 5
    Possible duplicate of [Converting string to date in js](http://stackoverflow.com/questions/5619202/converting-string-to-date-in-js) – John Mar 22 '17 at 09:41
  • 2
    It's not a valid timestamp. Valid date strings must be conform to ISO8601. – YounesM Mar 22 '17 at 09:41

2 Answers2

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);
Arkej
  • 2,221
  • 1
  • 14
  • 21
0

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");
Mantas
  • 89
  • 6