几点钟了?

时间:2018-06-26 02:42:52

标签: vb.net time compare

好吧,我为(美食时报)设计了项目,好吗? 我有五次吃饭

早晨中午日落夜晚午夜)< / p>


  • 早晨:03:20 AM
  • 中午:下午12:05
  • 日落:下午03:46
  • 晚上:07:33 PM
  • 午夜:晚上08:28

现在我需要放置计时器进行检查 它将当前时间与数组时间匹配 如果他找到阵列的时间到PC的当前时间,则显示一条消息


我实际上是在努力并编写了代码

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim TimesArray() As String = {"03:20 AM", "12:05 PM", "03:46 PM", "07:33 PM", "08:28 PM"}
    Dim NamesArray() As String = {"Morning", "Midday", "Sunset", "Night", "Midnight"}
    Dim time = DateTime.Now.TimeOfDay
    Dim q = TimesArray.Select(Function(t, i) New With {.Time = DateTime.Parse(t).TimeOfDay, .I = i}).Select(Function(d) New With {d.Time, .Diff = If(d.Time >= time, d.Time - time, New TimeSpan(24, 0, 0) + d.Time - time), d.I})
    Dim m = q.Min(Function(d) d.Diff)
    Dim r = q.First(Function(d) d.Diff = m)
    MsgBox("Food" & " " & NamesArray(r.I) & " " & "After" & " " & r.Diff.Hours & " " & "Hour" & " " & "and" & " " & r.Diff.Minutes & " " & "Minutes.")
End Sub

它可以100%工作,但是由于选择功能<,在 .Net Fremwork 3.5 中需要 System.Linq.dll 。 / strong> 这就是为什么我想要一些不需要此功能的东西(选择) 并且不需要 .Net fremwork 3.5

中的 System.Linq.dll

1 个答案:

答案 0 :(得分:0)

这应该满足您的需求。在每个计时器滴答声中,该子项都会遍历您的数组,并对照该数组检查当前时间,并且时差小于0小时0分钟,将显示消息框。

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim TimesArray() As String = {"03:20 AM", "12:05 PM", "03:46 PM", "07:33 PM", "08:28 PM"}
    Dim NamesArray() As String = {"Morning", "Midday", "Sunset", "Night", "Midnight"}

    For i As Integer = 0 To TimesArray.Count - 1
        Dim tempDateTime As Date = Date.Parse(TimesArray(i))
        Dim tempTimeDiff As TimeSpan = tempDateTime.Subtract(DateTime.Now)
        If tempTimeDiff.Hours = 0 And tempTimeDiff.Minutes = 0 Then
            MessageBox.Show("Food " & NamesArray(i) & " After " & tempDateTime.Hour & " and " & tempDateTime.Minute & " Minutes.")
        End If
    Next
End Sub