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?
- 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
5 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - datepicker parsing date issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741846626a2400818.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论