开发者

Change date format in javascript

I have an example which finds the difference of days between 2 specified dates and works only in Chrome, I think in firefox 6 and IE8 it does not support the format of date that I am using (shows NaN). Can somebody help me change the format to desired 1 (see below)?

Here is my DEMO

My format : 2011-08-18 11:49:01

Desired format : 08-18-2011 11:49:01

My Code

var cellvalue="2011-08-18 11:49:01.0 IST";
var firstDate = new Date();
var secondDate = cellvalue.substring(0, cellvalue.length-4);
alert(diffOf2Dates(firstDate,secondDate));
function diffOf2Dates(todaysDate,configDate)
{
/*var udate="2011-08-18 11:49:01.0";
var configDate=new Date(udate);*/

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = todaysDate; // Todays date
var secondDate = new Date(configDate);

var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));


return Math.ceil(diff开发者_Go百科Days);
}


I used the Datejs library with great results when having to work with dates in various formats. Give it a go: http://www.datejs.com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜