【问题标题】:Timespan between (2) other Timespan when adding / subtracting minutes C#加/减分钟 C# 时 (2) 其他时间跨度之间的时间跨度
【发布时间】:2015-05-13 17:08:27
【问题描述】:

如果我在“15 分钟”内通过,一切正常。我没有收到任何错误,只是我的 where 子句没有 100% 工作。这是我每隔 15 分钟及时通过的 b/c。

例子:

  • 对象 1 的时间为 00:20(上午 12:20)(24 小时制)
  • 对象 2 的时间为 02:15(凌晨 2:15)(24 小时制)

parsedTime 参数是 javascript 24 小时格式时间 - 在本例中为“00:15”。

问题是当我从 parsedTime 中减去 -30 分钟时,它会将它放在 23:45,因此永远不会得到“00:20”。

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

【问题讨论】:

  • 我不确定我是否理解,您的示例中的对象 1 和对象 2 在哪里?当你从上午 12:15 减去 30 分钟时,你预计会发生什么? (00:15)?
  • 对象 1 和 2 在 CMS 中,但两个对象都有一个 DateTime 属性,我只关心时间,而不关心日期。这里的日期根本不相关。当我从上午 12:15 减去 30 分钟时,.TimeofDay 将其变为“23:50”
  • 你还能检查日/月值吗?从技术上讲,代码所做的事情是 100% 正确的。在这种情况下,如果没有您所在日期的上下文,代码就会丢失 23:45 小于 00:20 的事实。单看那些时间,不考虑天,23:45不小于00:00,永远满足不了你的条件。
  • DateTimes,尽管包含 TimeOfDay 属性,但始终引用日期,当您减去 30 分钟时,您将得到前一天的 11:45 (23:45),它没有环绕。
  • 啊,我明白了。然后你可能需要对你的数学做一些额外的逻辑,以确保如果一个值“低于”00:00,它会被设置为一个基本的最小值;可能是 00:00。

标签: c# linq datetime timespan


【解决方案1】:

你只是想看看他们是否在 30 分钟之内,对吧?尝试使用实际时间跨度

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

【讨论】:

    【解决方案2】:

    听起来您还需要处理可能跨越午夜的时间范围,例如 "23:45""00:15" 之间的 30 分钟。这样做的方法如下:

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

    或者如果你喜欢更小的东西:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 2022-10-13
      相关资源
      最近更新 更多