...

# Difference in dates

javaone09
06-17-2010, 12:04 AM
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, 01:03 AM
Without other specifics, this is my best guess as to what you want.
Modify as desired for your requirements.

<html>
<title>Date Difference</title>
<script type="text/javascript">

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;
}
</script>
<body>

</body>
</html>

Good Luck!
:)

mrhoo
06-17-2010, 07: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);

Philip M
06-17-2010, 10: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