...

View Full Version : Difference in dates



javaone09
06-16-2010, 11:04 PM
Hi there

How can you calculate the difference in two dates that are in different formats?

I have one date as: '2010-06-16 21:20:44.000'

and the other date as now = new Date();

I want to subtract the dates to see if it is over 30 days.

Thanks!

jmrker
06-17-2010, 12:03 AM
Without other specifics, this is my best guess as to what you want.
Modify as desired for your requirements.


<html>
<head>
<title>Date Difference</title>
<script type="text/javascript">
// From: http://www.codingforums.com/showthread.php?t=198294

var SampleDates = [
'2010-06-16 21:20:44.000',
'2010-05-31 21:20:44.000',
'2010-05-16 21:20:44.000',
'2010-04-31 21:20:44.000',
'2010-04-16 21:20:44.000'
];
var now = new Date();

var then = new Date();
var ar1 = [];
var ar2 = [];
var diff;

var lengthOfDay = 1000*60*60*24;
for (var i=0; i<SampleDates.length; i++) {
ar1 = SampleDates[i].split(' ');
ar2 = ar1[0].split('-');
then = new Date(ar2[0],(ar2[1]-1),ar2[2]);
diff = now - then;
alert(SampleDates[i]+'\n'+now.toString()+'\n'+parseInt(diff/lengthOfDay)+' days');
}
</script>
</head>
<body>

</body>
</html>

Good Luck!
:)

mrhoo
06-17-2010, 06:03 AM
// If you just want whole days, and assume local time for both dates,
throw away the time part.


var dif, s='2010-06-26 21:20:44.000',
D= s.substring(0,10).split(/\D/);

D=new Date(+D[0],D[1]-1,+D[2]);
dif=Math.abs(new Date()-D);

alert(Math.floor(dif/86400000)+' days')

Philip M
06-17-2010, 09:54 AM
Another method (whole days):-


<script type = "text/javascript">
function timeDifference(laterdate,earlierdate) {
var difference = laterdate.getTime() - earlierdate.getTime();
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = daysDifference*1000*60*60*24
alert ('Difference = ' + daysDifference + ' days ');
}

var laterdate = new Date(2010,8,30);
var earlierdate = new Date(); // today

timeDifference(laterdate,earlierdate);
</script>


There are as many questions as there are answers. - Political commentator ITV



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum