Timespan between (2) other Timespan when adding / subtracting 分钟 C#
Timespan between (2) other Timespan when adding / subtracting minutes C#
如果我在“15 分钟”内通过,则一切正常。我没有收到任何错误,只是我的 where 子句没有 100% 有效。这是b/c我每隔15分钟就过去一次。
示例:
- 对象 1 的时间为 00:20(上午 12:20)(24 小时格式)
- 对象 2 的时间为 02:15(02: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();
您只是想看看他们之间的时间间隔是否在 30 分钟以内,对吧?尝试使用实际时间跨度
DateTime startTime;
DateTime parsedTime;
TimeSpan difference = startTime - parsedTime;
return difference.TotalMinutes < 30 && difference.TotalMinutes > -30;
听起来您还需要处理跨越午夜的时间范围,例如 "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);
}
如果我在“15 分钟”内通过,则一切正常。我没有收到任何错误,只是我的 where 子句没有 100% 有效。这是b/c我每隔15分钟就过去一次。
示例:
- 对象 1 的时间为 00:20(上午 12:20)(24 小时格式)
- 对象 2 的时间为 02:15(02: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();
您只是想看看他们之间的时间间隔是否在 30 分钟以内,对吧?尝试使用实际时间跨度
DateTime startTime;
DateTime parsedTime;
TimeSpan difference = startTime - parsedTime;
return difference.TotalMinutes < 30 && difference.TotalMinutes > -30;
听起来您还需要处理跨越午夜的时间范围,例如 "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);
}