如何查看基于午夜的日期范围

本文关键字:日期 范围 午夜 何查看 于午夜 | 更新日期: 2023-09-27 18:02:30

我试图找到两个时间之间的时间范围,并计算了从午夜开始的时差。

,

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

请帮忙好吗?

我正在看这个问题,但不确定它时间框架

如何查看基于午夜的日期范围

要知道这两个时间之间是否有午夜,只需检查日期是否不同:

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

如果您知道日期之间不会有超过一个午夜,则endDate1.Date是该午夜的时间。您可以使用以下命令获取午夜前后的时间:

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

试试这个

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

TimeSpan span = endTime;

控制台。Writeline("跨越:"+跨度);

控制台。Writeline("秒:"+ span.TotalSeconds);