vb.net dateDiff

Hi,

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
FCapoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HooKooDooKuCommented:
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.
0
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 ?
0
É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)
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi..
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
0
HooKooDooKuCommented:
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?

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

#2
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.