如何表示两个不同时间.NET之间的跨度

时间:2013-01-26 05:42:47

标签: .net vb.net

我正在通过.net构建一个闹钟。我希望通过百分比(进度条)显示警报接近触发的时间。我目前有一个功能可以让我从闹钟时间和当前系统时间倒计时返回小时,分钟和秒但我还没有能够解决这个问题..

Dim span As System.TimeSpan = TimeEnd.TimeOfDay - DateTime.Now.TimeOfDay
Dim remaining As String, remain As String, aTime As String

remaining = span.Hours & "hr:" & span.Minutes & "min:" & span.Seconds & "sec"
remain = span.Hours & span.Minutes & span.Seconds

For y = 0 To remain

    pBar.Minimum = 0
    pBar.Maximum = remain
    pBar.Value = y

    '  MsgBox("Min: " & pBar.Minimum & " Max:" & pBar.Maximum)
    Exit Sub


Next y

我想我需要澄清......

我有两次

闹钟时间:5:00:PM

当前时间:无论是什么

我需要计算两个值之间的时间量,并在进度条中表示两个值何时会满足,因为进度条会在接近时填满。

2 个答案:

答案 0 :(得分:1)

你遇到的一个主要问题是ProgressBar需要整数,而Timespan的Hour,Minute和Seconds属性只给出整数,即2分钟将返回2,而它将是120秒所以你需要将它们相乘在这种情况下我使用了秒。我创建了一个示例,其中DateTimePicker设置为Time,一个间隔为1秒的Timer,并创建了一个函数来给我两个DateTime对象的区别。看看是否适合你。

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = GetTimeDifference(DateTimePicker1.Value, DateTime.Now)
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Dim value As Integer = ProgressBar1.Maximum - GetTimeDifference(DateTimePicker1.Value, DateTime.Now)
        If value > ProgressBar1.Maximum Then
            Timer1.Stop()
            Exit Sub
        End If
        ProgressBar1.Value = value

    End Sub

    Public Function GetTimeDifference(EndTime As DateTime, StartTime As DateTime) As Integer
        Dim span As TimeSpan = EndTime.TimeOfDay - StartTime.TimeOfDay
        Dim hour As Integer = span.Hours * 360
        Dim minute As Integer = span.Minutes * 60
        Dim second As Integer = span.Seconds
        Dim result As Integer = hour + minute + second

        Return result

    End Function

End Class

每个康拉德的修改功能评论:

Public Function GetTimeDifference(EndTime As DateTime, StartTime As DateTime) As Integer

    Dim span As TimeSpan = EndTime.TimeOfDay - StartTime.TimeOfDay
    Dim result As Integer = CInt(span.TotalSeconds)

    Return result

End Function

答案 1 :(得分:0)

    Dim span As System.TimeSpan = TimeEnd.TimeOfDay - DateTime.Now.TimeOfDay
    Dim remaining As String, remain As String, aTime As String

    remaining = span.Hours & "hr:" & span.Minutes & "min:" & span.Seconds & "sec"
    remain = span.Hours & span.Minutes & span.Seconds

    For y = 0 To remain

        pBar.Minimum = 0
        pBar.Maximum = remain
        pBar.Value = y

        '  MsgBox("Min: " & pBar.Minimum & " Max:" & pBar.Maximum)
        Exit Sub


    Next y

我正在尝试将pbar.maximum设置为剩余时间,并在接近闹钟时间时让它进展。