admin管理员组

文章数量:1307634

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 es 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?

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 es 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?

Share Improve this question asked Aug 21, 2012 at 10:26 HelgusHelgus 1773 gold badges7 silver badges17 bronze badges 2
  • so the JS and jQuey are so weak? pfff – Helgus Commented Aug 21, 2012 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 Commented Dec 13, 2013 at 20:38
Add a ment  | 

5 Answers 5

Reset to default 4

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, '/'))

You are trying to parse a date in JavaScript, this is pletely 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()

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.

Firstly you need to reformat your ining datestring into something thats parsable by javascript Date() function. If you have no control of the format ing 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]));

本文标签: javascriptdatepicker parsing date issueStack Overflow