Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Difference in dates

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!

• Something to consider ...

Without other specifics, this is my best guess as to what you want.
Modify as desired for your requirements.
Code:
```<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!

• // If you just want whole days, and assume local time for both dates,
throw away the time part.

Code:
```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);

• Another method (whole days):-

Code:
```<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

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•