Jason Renaldo
Jason Renaldo

Reputation: 2822

Getting minutes from two DateTimes on different dates

I am comparing two DateTimes to see if there is 10 or fewer minutes between them. If I do DateTimeA - DateTimeB, and A is on 4/1/13 and B is on 4/3/13 I won't get the desired results.

I am only worried about minutes. So DateTimeA takes place at 8:00 am and DateTimeB takes place at 12:20 pm, I would want the result to be 260 minutes.

Upvotes: 2

Views: 157

Answers (3)

Julián Urbano
Julián Urbano

Reputation: 8488

(DateTimeA - DateTimeB).TotalMinutes % 24*60

Get the total number of minutes modulo the number of minutes in a day. That way you'll get rid of the different-day problem.

Upvotes: 6

Srikant Krishna
Srikant Krishna

Reputation: 881

(A - B).TotalMinutes

A-B will yield a Timespan, for which the total minutes can then be produced.

Upvotes: -1

Nomad101
Nomad101

Reputation: 1698

You will need to use DateTime.TimeofDay() To get the actual time values and then do arithmatic on them.

or use DateTime.Minute DateTime.Hour DateTime.Second. Here is a full explanation on DateTime objects: http://msdn.microsoft.com/en-us/library/system.datetime.aspx

Upvotes: 4

Related Questions