I've the next problem:
when I recieve date from server, I want to format it via datepicker, but datepicker throws exceptions, cause it can't parse the date.
here is my date, whcih comes from server(obj.value):
08.20.2012 19:01:32
and here is code via which I try to parse this date:
$.datepicker.formatDate('dd.MM.yy', new Date(obj.value));
I use MM cause I need the full name of the month.
and here is output after parsing:
NaN.NaN.NaN
so how to get rid of this exception?

- 177
- 3
- 7
- 17
-
so the JS and jQuey are so weak? pfff – Helgus Aug 21 '12 at 10:39
-
It is weakness of concrete IE8 implementation of Date object constructor. jQuery is not about dates, it is about DOM manipulation. – Konstantin Isaev Dec 13 '13 at 20:38
5 Answers
You need to change you date (obj.value
) to a valid JavaScript date format. Best if you can do on the server side.
If you want to do it on client side you need to replace .
with /
so you get 08/20/2012 19:01:32
instead of 08.20.2012 19:01:32
.
new Date(obj.value.replace(/\./g, '/'))

- 17,537
- 9
- 51
- 71
-
-
-
suggest 1000 000 users connect the page. server load and time cost money. and it's so simple, that JS is helpless? ;) – Helgus Aug 21 '12 at 10:43
-
Well as I said, you can parse the date on you client side code (in javascript) as in my answer above. – fredrik Aug 21 '12 at 10:45
-
it adds one more year )) console.log($.datepicker.formatDate('dd.MM.yy', new Date(obj.value.replace(/\./g, '/') ))); gives me 08.August.2013 – Helgus Aug 21 '12 at 10:48
-
What does console.log(new Date(obj.....) gives you? Also I think you need to first set the date and the set the format. See documentation http://jqueryui.com/demos/datepicker/ – fredrik Aug 21 '12 at 10:56
-
-
-
it says Date {Thu Aug 08 2013 19:01:32 GMT+0300 (FLE Daylight Time)} why day is 08 and not 20 ?? – Helgus Aug 21 '12 at 13:37
You are trying to parse a date in JavaScript, this is completely implementation depend. It seems that many browsers are unable to parse your provided string. You will have to do either of two thing:
- Provide a valid string.
- Call a Date constructor.
Also see this answer: Why does Date.parse give incorrect results?
Date()
receives integers
and strings
only. You should work the value of obj before passing it to Date()

- 732
- 1
- 7
- 14
Providing you have control over what is returned by the server, the simplest approach by far is to return a UNIX timestamp instead of a formatted date string.
UNIX epoch and Javascript epoch are the same (00:00 1 Jan 1970) but UNIX timestamps are in seconds while javascript's Date
object works with milliseconds.
So :
$.datepicker.formatDate('dd.MM.yy', new Date(obj.value * 1000));
As you can see HERE, obj.value can be integer or string. In the case of a string, javascript's automatic type conversion looks after the multiplication.

- 18,022
- 3
- 37
- 44
-
the incoming date is in the format above and NOT in the seconds from 1970 – Helgus Aug 21 '12 at 12:33
-
UNIX timestamp is pretty well the *de facto* format for transmission of date/time. Something to remember for the future. – Beetroot-Beetroot Aug 21 '12 at 18:50
Firstly you need to reformat your incoming datestring into something thats parsable by javascript Date() function. If you have no control of the format coming from you server i would suggest something along the lines of
var incDate, dateString, timeString, dateSplit, timeSplit;
incDate = obj.value.split(" ");
dateString = incDate[0];
timeString = incDate[1];
dateSplit = dateString.split(".");
timeSplit = timeString.split(":");
$.datepicker.formatDate('dd.MM.yy', new Date(dateSplit[2],dateSplit[0]-1,dateSplit[1],timeSplit[0],timeSplit[1],timeSplit[2]));

- 3,708
- 1
- 26
- 28
-
-
Are you sure you entered the dateSplit parts correctly as 2,0,1 the Date object requires it be entered in year,month,day descending order and if you had put 2,1,0 (2012,20,8) it would take 20 months and convert it to 1 year 8 months resulting in the 8/8/2013 date your currently seeing. Also please ensure you do not forget the `-1` on the month part as this is actually a 0-11 value (month 0 = january) – Simon West Aug 21 '12 at 12:51
-
I've just pasted your code and it doesn't work, then I added '-1' to dateSplit[2] and year is 2012. but why the day is 08 and not 20 ?? – Helgus Aug 21 '12 at 13:34
-
are you absolutly positive that the date your receiving is in the format `08.20.2012 19:01:32` and not `20.08.2012 19:01:32` the only thing i can think of is your actually receiving the latter. in which case you would need to adjust the dateSplit order to 2,1,0 – Simon West Aug 21 '12 at 13:44
-