datetimepicker检查当前时间

时间:2014-09-08 11:07:35

标签: .net time datetimepicker intervals

我有两个datetimepicker和3个标签看起来像这样

enter image description here

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim currentTime As TimeSpan = Date.Now.TimeOfDay

    If DateTimePicker1.Value <= Date.Now And Date.Now <= DateTimePicker2.Value Then
        Label3.Text = "In range"
    Else
        Label3.Text = "Not in range"
    End If
End Sub

我找到了检查datetimepickers的时间是否在当前时间范围内的方法。

我怎么知道结束前剩下多少时间以及从开始传递多少时间?

我希望你能理解。

编辑完成:

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblnow.Text = Date.Now.ToString("hh:mm:ss")
        Dim currentTime As TimeSpan = Date.Now.TimeOfDay
        Dim starttime As TimeSpan = DateTimePicker1.Value.TimeOfDay
        Dim endtime As TimeSpan = DateTimePicker2.Value.TimeOfDay

        If DateTimePicker1.Value <= Date.Now And Date.Now <= DateTimePicker2.Value Then
            Label3.Text = "In range"
            timeLeft.Text = endtime.Subtract(currentTime).ToString
            timePassed.Text = currentTime.Subtract(starttime).ToString
        Else
            Label3.Text = "Not in range"
        End If
    End Sub

如何不以此格式显示毫秒

1 个答案:

答案 0 :(得分:0)

你可以使用String.Remove

参考:String.Remove()

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim starttime As Date = DateTimePicker1.Value
    Dim endtime As Date = DateTimePicker2.Value

    Dim tmleft As TimeSpan = endtime.Subtract(Now)
    Dim tmPassed As TimeSpan = Now.Subtract(starttime)

    timeLeft.Text = tmleft.ToString.Remove(tmleft.ToString.IndexOf("."))
    timePassed.Text = tmPassed.ToString.Remove(tmPassed.ToString.IndexOf("."))
End Sub