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());
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());
Share
Improve this question
edited Jun 11, 2013 at 17:04
Tiago Sippert
1,3307 gold badges24 silver badges33 bronze badges
asked Aug 7, 2012 at 20:24
user007user007
1,7382 gold badges24 silver badges60 bronze badges
5
- 8 it's a date object, not a number, so if you're trying to cast it to int, it'll be NaN. But can't give a good answer without seeing a bit more of your code and what you're trying to do with it. Show us how you're getting NaN. – Spudley Commented Aug 7, 2012 at 20:26
- 1 In case it's related, there's a bug in Safari/iOS which prevents that format from working... stackoverflow./a/4310986/29 – Michael Haren Commented Aug 7, 2012 at 20:28
- 3 I works perfectly fine here: jsfiddle/jfriend00/RvAyu. There must be something else wrong with your code that you are not showing us. – jfriend00 Commented Aug 7, 2012 at 20:28
- The above code does not work in IE, works in Chrome. I am sorry for not mention that here. I am using IE 9 and my organization's default browser is IE 8 or 9. – user007 Commented Aug 7, 2012 at 21:13
-
@Spudley 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());
– user007 Commented Aug 7, 2012 at 21:17
3 Answers
Reset to default 6Add "T" to the date format. e.g:
new Date('2011-12-15T00:00:00')
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 remend. 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));
The easy solution I tried Download date.js from http://datejs./ 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744707137a4589121.html
评论列表(0条)