0

i want to convert this type of datetime 2021-07-05 17:41:22 to time number 1626351219837

let str_data = '2021-07-05 17:41:22';
let stringDate = new Date(str_data).getTime();

console.log(stringDate);

gave a result NaN

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Anthony phillips
  • 152
  • 3
  • 13
  • It works for me (on Chrome and Firefox, that is). The question is, what browser are you using it on? I know Safari has slightly different parsing behavior: see https://stackoverflow.com/questions/21883699/safari-javascript-date-nan-issue-yyyy-mm-dd-hhmmss – Terry Jul 15 '21 at 12:27
  • 1
    `'2021-07-05 17:41:22'` is not a spec compliant format. It very well might be rejected depending on the environment. You need a `T` between the date and time: `'2021-07-05T17:41:22'`. Do be ware that it will be interpreted as the local time zone. – VLAZ Jul 15 '21 at 12:31
  • your suggestion really helped me. i splitted the space, then created a new date array with the T included and it worked perfectly – Anthony phillips Jul 15 '21 at 12:36
  • To add, if you do want this in UTC time, you could also add a `Z` – Keith Jul 15 '21 at 12:37

0 Answers0