简体   繁体   English

加/减分钟C#时(2)个其他时间跨度之间的时间跨度

[英]Timespan between (2) other Timespan when adding / subtracting minutes C#

Everything works with the exception if I pass in "15 minutes". 如果我在“ 15分钟”内通过,一切都会例外。 I receive no errors, it's just that my where clause isn't working 100%. 我没有收到任何错误,只是我的where子句不能100%工作。 This is b/c I pass in time in 15 minute intervals. 这是我以15分钟为间隔的时间。

Example: 例:

  • Object 1 has a time of 00:20 (12:20 am) (24hr format) 对象1的时间为00:20(上午12:20)(24小时格式)
  • Object 2 has a time of 02:15 (02:15 am) (24hr format) 对象2的时间为02:15(上午02:15)(24小时格式)

The parsedTime param is a javascript 24hr format time - in this example is comes in as "00:15". parsedTime参数是javascript 24小时格式的时间-在此示例中为“ 00:15”。

The problem is when I subtract the -30 minutes from the parsedTime , it puts it at 23:45, and therefore never gets the "00:20". 问题是,当我从parsedTime中减去-30分钟时,它将其放置在23:45处,因此从不获取“ 00:20”。

LINQ query LINQ查询

DateTime parsedTime = DateTime.ParseExact(time, "HH:mm", CultureInfo.InvariantCulture);

var activities = objects
          .Where(x => (x.GetValue<DateTime>("startTime").TimeOfDay 
                      >= parsedTime.AddMinutes(-30).TimeOfDay
                 && x.GetValue<DateTime>("startTime").TimeOfDay 
                     <= parsedTime.AddMinutes(30).TimeOfDay))
          .ToList();

You just want to see if they're within 30 minutes of each other, right? 您只想看看它们是否在30分钟内,对吗? Try using actual timespans 尝试使用实际时间跨度

DateTime startTime;
DateTime parsedTime;
TimeSpan difference = startTime - parsedTime;
return difference.TotalMinutes < 30 && difference.TotalMinutes > -30;

It sounds like you also need to handle time ranges that could span across midnight, as in the 30 minutes that exists between "23:45" and "00:15" . 听起来您还需要处理可能跨越午夜的时间范围,例如"23:45""00:15"之间的30分钟。 Here's how you can do that: 这是您可以执行的操作:

public static TimeSpan GetTimeDifference(string startTimeOfDay, string endTimeOfDay)
{
    DateTime startDateTime = DateTime.ParseExact(startTimeOfDay, "HH:mm",
        CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault);

    DateTime endDateTime = DateTime.ParseExact(endTimeOfDay, "HH:mm",
        CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault);

    if (endDateTime >= startDateTime)
    {
        // values do not cross over midnight
        return endDateTime - startDateTime;
    }
    else
    {
        // values cross over midnight
        return endDateTime - startDateTime + TimeSpan.FromHours(24);
    }
}

Or if you prefer something smaller: 或者,如果您更喜欢小东西:

public static int GetMinutesDifference(string startTimeOfDay, string endTimeOfDay)
{
    DateTime startDateTime = DateTime.ParseExact(startTimeOfDay, "HH:mm",
        CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault);

    DateTime endDateTime = DateTime.ParseExact(endTimeOfDay, "HH:mm",
        CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault);

    return (((int)(endDateTime - startDateTime).TotalMinutes + 1440) % 1440);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM