获取特定时间段之间的总小时数

本文关键字:小时 之间 定时间 时间段 获取 | 更新日期: 2023-09-27 18:15:30

条件是Time in and Time out(例如2015年1月2日02:55'til 2015年2月2日05:55(,即超过一天。我已经计算了Time in和Time out的总小时数,我想知道总小时数是否在23:00(11:00PM(到06:00AM之间过去,并获得其总数

获取特定时间段之间的总小时数

var hours = (datevalue1 - datevalue2).TotalHours;

Timespace ts= (datevalue1 - datevalue2);
var hours = ts.Value.TotalHours;

试试这种方法。。DateTime.Parse().Subtract()

例如:

string startTime = "11:00 PM";
string endTime = "6:00 AM";
TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
Console.WriteLine(duration);
Console.ReadKey();

TimeSpan是您需要的对象:

TimeSpan span = (DateTime.Now - DateTime.Now);
String.Format("{0} days, {1} hours, {2} minutes, {3} seconds", 
    span.Days, span.Hours, span.Minutes, span.Seconds);

您可以通过传递时间来计算它。当其夜间时间将其添加到CCD_ 3时。

DateTime timeIn = new DateTime(2015, 09, 29, 10, 11, 3); // 29-09-2015 at 10:11:03
DateTime timeOut = new DateTime(2015, 10, 1, 2, 19, 18); // 01-10-2015 at 02:19:38
TimeSpan nightTime = new TimeSpan(); //total amount of night time
TimeSpan passLength = new TimeSpan(0, 0, 0, 1); // length of time to pass at each iteration (1s)
while (timeIn < timeOut) // do it until timeIn reaches timeOut
{
    timeIn = timeIn.Add(passLength); // add 1 second to timeIn
    if (timeIn.Hour < 6 || timeIn.Hour == 23) // if we are in range of night time
    {
        nightTime = nightTime.Add(passLength); // add 1 second to night time
    }
}
Console.WriteLine(nightTime);

你可以做很多优化。在很长一段时间内,每次增加1秒不是一个好主意。您可以在每次迭代时向TimeIn添加1天,然后仅向夜间添加6小时。接近超时后,减少的长度时间

这里有一个更好的方法。第一天过得很快。那就剩下的时间。

DateTime timeIn = new DateTime(2015, 09, 29, 10, 11, 3); // 29-09-2015 at 10:11:03
DateTime timeOut = new DateTime(2015, 10, 1, 2, 19, 18); // 01-10-2015 at 02:19:38
// Get days
TimeSpan passLength = new TimeSpan(1, 0, 0, 0); // one day per iterate
while (timeIn + passLength < timeOut)
{
    timeIn = timeIn.Add(passLength);
    nightTime = nightTime.Add(new TimeSpan(0, 7, 0, 0)); // 7 hours of a day passed is considered night time
}
// Get rest of the time
passLength = new TimeSpan(0, 0, 0, 1); // one second per iterate
while (timeIn < timeOut) // do it until timeIn reaches timeOut
{
     timeIn = timeIn.Add(passLength); // add 1 second to timeIn
     if (timeIn.Hour < 6 || timeIn.Hour == 23) // if we are in range of night time
     {
         nightTime = nightTime.Add(passLength); // add 1 second to night time
     }
}
Console.WriteLine(nightTime);

您不应该担心剩余时间的计算性能。因为剩余时间现在小于1天,这仅仅是CCD_ 4秒。

对于今天的处理器速度来说,少于86400的迭代应该是可以的。你如何仍然可以在更远的地方优化它,但你不会得到更多的性能。

一种稍微不同且更快的方法:

    static void Main(string[] args)
    {
        TimeSpan result = new TimeSpan();
        DateTime dt1 = new DateTime(2015, 09, 29, 10, 11, 03);
        DateTime dt2 = new DateTime(2015, 10, 01, 02, 19, 38);
        DateTime d1 = new DateTime(dt1.Year, dt1.Month, dt1.Day, 0, 0, 0); //Date only
        DateTime d2 = new DateTime(dt2.Year, dt2.Month, dt2.Day, 0, 0, 0); //Date only
        //Count night time in first day
        result += DateTime.Compare(dt1, d1.AddHours(6)) > 0 ? new TimeSpan(6, 0, 0) : new TimeSpan(dt1.Hour, dt1.Minute, dt1.Second);
        if (DateTime.Compare(dt1, d1.AddHours(23)) > 0) result += new TimeSpan(dt1.Hour - 23, dt1.Minute, dt1.Second);
        //Count night time in last day
        result += DateTime.Compare(dt2, d2.AddHours(6)) > 0 ? new TimeSpan(6, 0, 0) : new TimeSpan(dt2.Hour, dt2.Minute, dt2.Second);
        if (DateTime.Compare(dt2, d2.AddHours(23)) > 0) result += new TimeSpan(dt1.Hour - 23, dt2.Minute, dt2.Second);
        //Count night time in middle days
        int daysBetween = (int)(d2 - d1).TotalDays - 1;
        result += new TimeSpan(daysBetween * 7, 0, 0);
        Console.WriteLine("Night time: " + result);
        Console.ReadKey();
    }

EndTime与您的范围(23:00-06:00) 进行比较

即在您的情况下,检查EndTime 05:55<06:00EndTime 05:55>23:00

您可以减去DateTime的值,得到介于两者之间的TimeSpan。然后你可以在中获得TotalHours

var hours = timeOut.Subtract(timeIn).TotalHours;

例如

timeIn = 29-09-2015 10:11:03;
timeOut = 01-10-2015 02:19:38;
hours = 52.14303137125;