Reputation: 1730
The code: new Date('2011-12-15 00:00:00')
is showing as NaN
.
How can I convert this date?
Any helps are appreciated.
My code is very straight forward. It works in Chrome but not in IE 9.
var dateText = new Date('2012-08-01 00:00:00');
alert(dateText.getDate().toString() + "/" + dateText.getMonth().toString() + "/" + dateText.getYear().toString());
Upvotes: 4
Views: 3455
Reputation: 587
The easy solution I tried Download date.js from http://datejs.com/ Include in your file then var date = Date.parse('1970-01-12 00:00:00'); var formattedDate = date.toString('yyyy-MM-dd');
It works like charm in Safari.
Upvotes: 0
Reputation: 1730
After some search in web, realized that the stupid IE does not understand this date format. I do not know about Safari and Firefox, but certainly this works in Chrome.
Either I will have to use some javascript libraries like DateJS for doing this or will have to do some custom coding as below which I will never recommend. Fortunately in my case, I am sure that I will be getting the dates in the YYYY-MM-DD HH:MI:SS format.
var dateText = '2011-12-15 00:00:00';
alert(dateText.substring(5, 7) + "/" + dateText.substring(8, 10) + "/" + dateText.substring(0, 4));
Upvotes: 0
Reputation: 39777
Add "T" to the date format. e.g:
new Date('2011-12-15T00:00:00')
Upvotes: 6