我想显示两个日期之间的日期和时间差异。我坚持使用这段代码:(
看看这段代码:
Dim d1 As DateTime = "2/13/2018 1:50:00 PM"
Dim d2 As DateTime = "2/20/2018 1:50:00 PM"
Dim count = Enumerable.Range(0, (d2 - d1).TotalHours).
Select(Function(i) d1.AddHours(i).DayOfWeek).
Where(Function(d) d <> DayOfWeek.Sunday And d <> DayOfWeek.Saturday).
Count()
MessageBox.Show("Time Consumed: " & count)
我想要的输出应该是这样的。 40:00:00
不包括星期日和星期六以及每个星期的时间段仅适用于上午8:00 - 下午5:00
答案 0 :(得分:2)
我很喜欢Enigmativity的答案,所以我想我会扩展它:
Dim minutes =
Enumerable _
.Range(0, CInt(d2.Subtract(d1).TotalMinutes)) _
.Select(Function(m) d1.AddMinutes(m)) _
.Where(Function(d) d.Hour >= 8 And d.Hour < 17) _
.GroupBy(Function(d) d.DayOfWeek) _
.Where(Function(g) g.Key <> DayOfWeek.Saturday And g.Key <> DayOfWeek.Sunday) _
.Select(Function(g) g.Count()) _
.Sum()
MessageBox.Show("Time Consumed: " & Math.Floor(minutes / 60) & " hours " & minutes Mod 60 & " mins")
答案 1 :(得分:0)
这样做吗?
Dim d1 As DateTime = DateTime.Parse("2/13/2018 1:50:00 PM")
Dim d2 As DateTime = DateTime.Parse("2/20/2018 1:50:00 PM")
Dim count = _
Enumerable _
.Range(0, Cint(d2.Subtract(d1).TotalHours)) _
.Select(Function(i) d1.AddHours(i).DayOfWeek) _
.Where(Function(d) d <> DayOfWeek.Sunday And d <> DayOfWeek.Saturday) _
.Distinct() _
.Count() * 8
注意:请为您的代码转Option Strict On
- 它会帮助您成为更好的程序员。