Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Something to consider ...

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

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    // 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);
    
    alert(Math.floor(dif/86400000)+' days')

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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
    Last edited by Philip M; 06-17-2010 at 10:05 AM.


  •  

    Posting Permissions

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