• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

vb.net dateDiff


I have 2 specific "date" columns and I want to know the difference in time between the two, I don't want to consider the actual different days, only the time!

For example, one entry is at 8:00 Am on monday and the other is at 10:00 am on tuesday, I want to calculate only the difference in time to obtain "2 hours" and not "26 hours"

currently I have :
DateDiff(DateInterval.Minute, controleAccessBindingsource.Item(controleAccessBindingsource.Position)("debut"), controleAccessBindingsource.Item(controleAccessBindingsource.Position)("fin")) / 60

but this considers the days as well
1 Solution
If I'm understanding you correctly, you basically are doing something like the following pseudo-code:
Hours = DateDiff( In Minutes, Date1, Date2 ) / 60

The first thing that jumps to mind is to simply do a MOD operation on the result (i.e. what is the remainder after division):

Hours = ( DateDiff( In Minutes, Date1, Date2 ) / 60 ) MOD 24

That will take your value of 26 hours, divide it by 24, and give you the remainder of 2.
FCapoAuthor Commented:
it's not always 24 hours sometimes there's weeks between the 2 dates!

Isn't there a simple way to take the "Time portion" of a date and compare it to another "Time portion" of another date? And treat them as both times are in one day?

If I have 2 dates :

9/18/2013 5:05 P.M
9/5/2013 4:05 P.M

I want to do 5:05 P.M - 4:05 P.M = 1 hour ?
Éric MoreauSenior .Net ConsultantCommented:
you should keep only the time part of your variables:

        Dim d1 As DateTime = New DateTime(2013, 9, 18, 17, 5, 0)
        Dim d2 As DateTime = New DateTime(2013, 9, 5, 16, 5, 0)

        Dim newd1 As DateTime = New DateTime(1, 1, 1, d1.Hour, d1.Minute, d1.Second)
        Dim newd2 As DateTime = New DateTime(1, 1, 1, d2.Hour, d2.Minute, d2.Second)
        Dim hours As Long = DateDiff(DateInterval.Hour, newd1, newd2)
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
You Can try this

DateTime startTime = DateTime.Now;

DateTime endTime = DateTime.Now.AddSeconds( 75 );

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
Console.WriteLine( "Time Difference (hours): " + span.Hours );
Console.WriteLine( "Time Difference (days): " + span.Days );

Output Like,

Time Difference (seconds): 15
Time Difference (minutes): 1
Time Difference (hours): 0
Time Difference (days): 0
it's not always 24 hours sometimes there's weeks between the 2 dates!
It doesn't matter.  The MOD operator does not subtract 24 hours, it divides the date diff by 24 hours and gives you the remainder (i.e. what is the time difference).

Date1 = 01-Sep-2013 2:00
Date2 = 19-Sep-2013 4:00
Minutes = DateDiff( In Minutes, Date2, Date1 ) ==> 26,040
Hours  = Minutes / 60 ==> 434
HoursDiff = Hours MOD 24 ==> 2

The only short coming is to realize that the MOD operator only works on integers.  So to get the best resolution, take a MOD with number of minutes in a day (since that is what DateDiff is giving you).

Since there are 1,440 minutes in a date:
Date1 = 01-Sep-2013 2:00
Date2 = 19-Sep-2013 3:30
Minutes = DateDiff( In Minutes, Date2, Date1 ) ==> 26,010
MinOffset = Minutes MOD 1440 ==> 90

Isn't there a simpler way...?
Which looks simpler?

Time1 = GetTime( Date1 )
Time2 = GetTime( Date2 )
TimeSpan = DateDiff( Time1, Time2 )

TimeSpan = DateDiff( Date1, Date2 ) MOD Day

So my suggestion would to modify the original formula I provided to this:
Minutes = DateDiff( In Minutes, Date1, Date2 ) MOD 1440

That will give you the number of minutes between the two dates irrespective of the number of days between Date1 and Date2.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now