Usher
Usher

Reputation: 2146

How to check Date range based on midnight?

Am trying to find the time range in between two time and calculated the time difference from midnight.

Eg:

StartDate1 : 13/02/2013 21.00
EndDate1:    13/02/2013 22.00.

StartDate2 : 13/02/2013 23.00
EndDate2:    14/02/2013 01.00.

1) am trying to find the time range, it falls in to midnight first ?
2) If yes then how much time difference from Midnight 
    e.g Day1= 2hrs (prior to midnight)
        Day2= 1hr (after midnight).

Any help please ?

I was looking at this question but not sure about it TimeFrame

Upvotes: 0

Views: 2628

Answers (3)

Guffa
Guffa

Reputation: 700212

To find out if there is a midnight between the times, just check if the dates are different:

if (startDate1.Date != endDate1.Date) {
  // there is at least one midnight between the times
}

If you know that there is never more than one midnight between the dates, then endDate1.Date is the time of that midnight. You can get the times before and after midnight using:

TimeSpan before = endDate1.Date - startDate1;
TimeSpan after = endDate1 - endDate1.Date;

Upvotes: 2

ZZZ
ZZZ

Reputation: 2812

TimeSpan span = endTime - startTime;

Console.Writeline("Span: "+span);

Console.Writeline("Seconds: "+span.TotalSeconds);

Upvotes: 0

Roshana
Roshana

Reputation: 428

Try this.

 DateTime startTime = DateTime.Parse("13/02/2013 22.00");

 DateTime endTime = DateTime.Parse("14/02/2013 01.00");

 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 );

Upvotes: 2

Related Questions