How to calculate time elapsed between two dates

Discussion in 'Visual Basic .NET' started by KamBhai, Apr 1, 2013.

  1. KamBhai

    KamBhai Power Member

    Joined:
    Aug 13, 2010
    Messages:
    742
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    IM
    Location:
    Sweet Home
    Hello guys,
    Can anyone help me with this?
    If you can't write the program then it's fine just give me the logic and I will do myself.

    I need to calculate the duration between 2 given dates.

    Suppose, I choose 11/1/1992 and 8/6/2001
    It should give the output as X year X month X days.

    I know it's simple, but I am not getting the logic yet. Also don't forget the leap years.

    Thanks in advance !
     
  2. switch_2012

    switch_2012 Power Member

    Joined:
    Feb 28, 2010
    Messages:
    504
    Likes Received:
    228
    Occupation:
    IM
    Location:
    UK
    I think you will need flux capacitor to run this program on:)
     
  3. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,870
    Likes Received:
    3,385
    Gender:
    Male
    Occupation:
    Dreaming
    Location:
    Pale Blue Dot
    Home Page:
    It's easy in PHP :
    Code:
    <?php
    
    function dateDiff($d1,$d2){
        $date1=strtotime($d1);
        $date2=strtotime($d2);
        $seconds = $date1 - $date2;
        $weeks = floor($seconds/604800);
        $seconds -= $weeks * 604800;
        $days = floor($seconds/86400);
        $seconds -= $days * 86400;
        $hours = floor($seconds/3600);
        $seconds -= $hours * 3600;
        $minutes = floor($seconds/60);
        $seconds -= $minutes * 60;
        $months=round(($date1-$date2) / 60 / 60 / 24 / 30);
        $years=round(($date1-$date2) /(60*60*24*365));
        $diffArr=array("Seconds"=>$seconds,
                      "minutes"=>$minutes,
                      "Hours"=>$hours,
                      "Days"=>$days,
                      "Weeks"=>$weeks,
                      "Months"=>$months,
                      "Years"=>$years
                     ) ;
       return $diffArr;
    }
    
    print_r(dateDiff("2012-02-21 11:11:11","2011-01-01 10:09:11"));
    ?>
    
    
     
  4. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,876
    Likes Received:
    12,649
    Occupation:
    Potentate
    Location:
    Asuncion
    • Thanks Thanks x 2
  5. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,870
    Likes Received:
    3,385
    Gender:
    Male
    Occupation:
    Dreaming
    Location:
    Pale Blue Dot
    Home Page:
    Oh sorry I just googled and got this snippset lol :p
    On a serious note, it's almost impossible to memorize all the functions. So, I don't care at all! It doesn't harm coz I have netbeans :)

    EDIT: That won't support for PHP < 5.3 :(
     
    Last edited: Apr 1, 2013
  6. bertbaby

    bertbaby Elite Member

    Joined:
    Apr 15, 2009
    Messages:
    2,020
    Likes Received:
    1,503
    Occupation:
    Product marketing
    Location:
    USA
    Home Page:
    How about holidays and weekends?
     
    • Thanks Thanks x 1
  7. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,876
    Likes Received:
    12,649
    Occupation:
    Potentate
    Location:
    Asuncion
    I don't remember them either, it 's silly to remember them all. What you should remember though is what PHP functions are available in general. In this case for example, what I had in mind is that the DateTime class had a diff functionality so I opened up the manual to link to it. Modern IDEs do the job of remembering the syntax for us :)
     
    • Thanks Thanks x 2
  8. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,870
    Likes Received:
    3,385
    Gender:
    Male
    Occupation:
    Dreaming
    Location:
    Pale Blue Dot
    Home Page:
    Thanks for the tip man. I will try them out for sure :)
    EDIT:: N yeah, it really looks like PHP has become a graduate. I am discovering so many static methods these days. I really think I should go back to the school lol.
     
    • Thanks Thanks x 1
    Last edited: Apr 1, 2013
  9. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,870
    Likes Received:
    3,385
    Gender:
    Male
    Occupation:
    Dreaming
    Location:
    Pale Blue Dot
    Home Page:
    That's a system accumulated error, which needs to be rectified manually.
     
  10. mirzetaga

    mirzetaga Newbie

    Joined:
    Aug 20, 2011
    Messages:
    2
    Likes Received:
    1
    Code:
            Dim from As Date = DateTimePicker1.Value        Dim toDays As Date = DateTimePicker2.Value
            Dim years As Integer = toDays.Year - from.Year
            Dim mm As Integer = toDays.Month - from.Month
            If mm < 0 Then
                years = years - 1
                mm = 12 + mm
            End If
            Dim days As Integer = toDays.Day - from.Day
            If days < 0 Then
                mm = mm - 1
                days = days + Date.DaysInMonth(from.Year, from.Month)
            End If
            MsgBox("Y: " & years & " M: " & mm & " D: " & days)
    Is this what you need?

    Regards
     
    • Thanks Thanks x 1