-2

My code is like this :

createDate = '2016-07-12 09:09:38';
createDate = new Date(createDate);
console.log(createDate);

The result : Date {Invalid Date}

I want the result like this : Date {Tue Jul 12 2016 09:53:13 GMT+0700 (SE Asia Standard Time)}

Any solution to solve my problem?

moses toh
  • 12,344
  • 71
  • 243
  • 443

1 Answers1

1

Use this ISO-8601 format:

createDate = '2016-07-12T09:09:38';   //watch for the T in between
createDate = new Date(createDate);
console.log(createDate);

here UTC time zone used to interpret arguments.

Check This

Asif Rahaman
  • 775
  • 6
  • 10
  • If `createDate` is dinamyc. And the format like this : `createDate = '2016-07-12 09:09:38';`. How to change `createDate = '2016-07-12 09:09:38';` to `createDate = '2016-07-12T09:09:38';`? – moses toh Jul 12 '16 at 03:14
  • you can use `createDate.replace(" ", "T");` assuming you have dynamic date string in `createDate` variable. – Asif Rahaman Jul 12 '16 at 03:17
  • It's working in Firefox. But in Chrome, It's not working – moses toh Jul 12 '16 at 05:04
  • @mosestoh I've tested it myself in chrome and it's working fine. try debugging. print `createDate` after using `replace()`. make sure that your dynamic data has only one space between the date and time. – Asif Rahaman Jul 12 '16 at 05:09
  • Thank you very much – moses toh Jul 12 '16 at 06:52
  • I need you help. Look here : http://stackoverflow.com/questions/38322089/why-convert-string-date-to-date-javascript-not-working-in-chrome – moses toh Jul 12 '16 at 07:05