在C#中,从23:00:00到24:00:00之间的最近时间为24:00:00

时间:2014-12-01 12:29:29

标签: c# datetime if-statement

我试图让两个值之间的时间接近24:00:00,午夜之前和午夜之后。

编辑:这只是我想要做的一个例子。在这种情况下,我应该得到两个项目。

var dt1 = new DateTime(2014, 11, 11, 23, 50, 00);
var dt2 = new DateTime(2014, 12, 11, 00, 50, 00);
var l = new List<DateTime>();

for (int i = 0; i < l.Count - 1; i++)
{
    TimeSpan ts1 = new TimeSpan(l[i].Hour, l[i].Minute, l[i].Second);
    TimeSpan ts2 = new TimeSpan(l[i + 1].Hour, l[i + 1].Minute, l[i + 1].Second);

    if (ts1.TotalHours <= 23 && ts2.TotalHours >= 00)
    {
        Console.WriteLine("00:00:00 - {0} {1} \n", ts1, ts2);
    }
}

感谢您的帮助和建议。

2 个答案:

答案 0 :(得分:2)

你的问题很混乱,并不完全清楚你想要实现的是什么,但我做了一些假设,并提出了我认为可能是你所追求的事情:

var l = new List<DateTime> {
    new DateTime(2014, 11, 11, 22, 0, 0),
    new DateTime(2014, 11, 11, 23, 45, 0),
    new DateTime(2014, 11, 11, 23, 55, 0),
    new DateTime(2014, 11, 11, 23, 59, 59),
    new DateTime(2014, 11, 12, 0, 0, 0),
    new DateTime(2014, 11, 12, 0, 4, 0),
    new DateTime(2014, 11, 12, 0, 15, 0),
    new DateTime(2014, 11, 12, 1, 0, 0),
    new DateTime(2014, 11, 12, 10, 0, 0),
};

for (int i = 0; i < l.Count - 1; i++) {

    if (l[i].TimeOfDay.TotalMinutes < 5 || l[i].TimeOfDay.TotalMinutes >= 23*60 + 55) 
        Console.WriteLine("{0} is close to midnight", l[i]);
    else
        Console.WriteLine("{0} is NOT close to midnight", l[i]);

}

我已经用一些测试数据加载了日期/时间列表,并且代码只是打印出每个日期/时间是否在午夜两侧的5分钟内。

另一个回答你问题的另一个尝试是如下:

var l = new List<DateTime> {
    new DateTime(2014, 11, 11, 15, 0, 0),   // 15:00:00
    new DateTime(2014, 11, 11, 16, 0, 0),   // 16:00:00
    new DateTime(2014, 11, 11, 17, 0, 0),   // 17:00:00
    new DateTime(2014, 11, 11, 17, 20, 0),  // 17:20:00
    new DateTime(2014, 11, 11, 18, 15, 0),  // 18:15:00
    new DateTime(2014, 11, 11, 19, 0, 0),   // 19:00:00
    new DateTime(2014, 11, 11, 22, 0, 0),   // 22:00:00
    new DateTime(2014, 11, 11, 23, 45, 0),  // 23:45:00
    new DateTime(2014, 11, 11, 23, 50, 00), // 23:50:00
    new DateTime(2014, 12, 11, 00, 50, 00), // 00:50:00
    new DateTime(2014, 11, 12, 1, 0, 0),    // 01:00:00
    new DateTime(2014, 11, 12, 10, 0, 0),   // 10:00:00
};

var time = new TimeSpan(18, 0, 0); // <- Set the target time here
var offsetBefore = new TimeSpan(1, 0, 0, 0).TotalMilliseconds - time.TotalMilliseconds;
var offsetAfter = time.TotalMilliseconds * -1;

var closestBefore =
    l.Aggregate(
        (current, next) =>
            next.AddMilliseconds(offsetBefore).TimeOfDay.TotalMilliseconds > current.AddMilliseconds(offsetBefore).TimeOfDay.TotalMilliseconds
                ? next
                : current);

var closestAfter =
    l.Aggregate(
        (current, next) =>
            next.AddMilliseconds(offsetAfter).TimeOfDay.TotalMilliseconds < current.AddMilliseconds(offsetAfter).TimeOfDay.TotalMilliseconds
                ? next
                : current);

Console.WriteLine("{0} is the closest date/time before {1}.", closestBefore, time);
Console.WriteLine("{0} is the closest date/time after {1}.", closestAfter, time);
Console.WriteLine("00:00:00 - {0} {1} \n", closestBefore, closestAfter);

// OUTPUTS:
//   11/11/2014 17:20:00 is the closest date/time before 18:00:00.
//   11/11/2014 18:15:00 is the closest date/time after 18:00:00.
//   00:00:00 - 11/11/2014 17:20:00 11/11/2014 18:15:00    

这会将列表中最接近的日期/时间返回午夜之前的午夜,并将列表中最接近的日期/时间返回午夜之后的午夜。

希望这有帮助!

答案 1 :(得分:1)

试试这个:

        var dt1 = new DateTime(2014, 11, 11, 23, 50, 00);
        var dt2 = new DateTime(2014, 12, 11, 00, 50, 00);

        var dt1temp = new DateTime(dt1.Year, dt1.Month, dt1.Day, 00, 00, 00);
        var dt2temp = new DateTime(dt2.Year, dt2.Month, dt2.Day, 00, 00, 00);

        TimeSpan time1 = new TimeSpan();
        TimeSpan time2 = new TimeSpan();
        TimeSpan time24 = new TimeSpan(24, 0, 0);

        time1 = dt1 - dt1temp;
        time2 = dt2 - dt2temp;

        if (time1.Hours >= 12) time1 = time24 - time1;
        if (time2.Hours >= 12) time2 = time24 - time2;

        string result = "";

        if (time1 < time2) result = "Time1 nearer to 00:00";
        else result = "Time2 nearer to 00:00";