AabinGunz
AabinGunz

Reputation: 12347

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(diffDays);
}

Upvotes: 0

Views: 474

Answers (1)

CyberDude
CyberDude

Reputation: 8949

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

Upvotes: 2

Related Questions