PDA

View Full Version : How to calculate time between two dates

NDK
06-19-2009, 09:09 AM
Hi,

i'm trying to calculate the time between two dates. I already searched on internet, and i always see the same answer.

But if i do it, it's not right...

For example:

Current date = 2009,6,19
date = 2009,7,22

var date1=new Date(19,5,2009);
var date2=new Date(22,6,2009)
//I do -1 because in javascript: January is 0 and December 11

date2 = -1325638800000
date1 = -1420333200000
Dif: 1096
= Math.abs((date1.getTime()-date2.getTime())/(one_day)

What is wrong?

forum_amnesiac
06-19-2009, 12:50 PM
Have a look at this code I found

t1="10/10/2006" ;

t2="15/10/2006";

//Total time for one day
var one_day=1000*60*60*24;
//Here we need to split the inputed dates to convert them into standard format
for furter execution
var x=t1.split("/");
var y=t2.split("/");
//date format(Fullyear,month,date)

var date1=new Date(x[2],(x[1]-1),x[0]);

var date2=new Date(y[2],(y[1]-1),y[0])
var month1=x[1]-1;
var month2=y[1]-1;

//Calculate difference between the two dates, and convert to days

_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
//_Diff gives the diffrence between the two dates.