none
I Want to make a timer to the end of the year at my school and I don't know how I can make it so its in Days,Hours,Minutes,Seconds RRS feed

  • Question

  • This is my code 

    Public Class Form1

        Dim MyDate As Date = CDate("13 Dec 2019")

        Dim sec As Integer

        Dim Secs As Integer

        Dim Mins As Integer

        Dim Hours As Integer

        Dim Days As Integer


        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim idelay As Integer = 1
            Timer1.Interval = idelay
            Timer1.Enabled = True
        End Sub



        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

            Hours = DateDiff(DateInterval.Hour, Now(), MyDate).ToString

            Mins = DateDiff(DateInterval.Minute, Now(), MyDate).ToString
            Secs = DateDiff(DateInterval.Second, Now(), MyDate).ToString
            Days = DateDiff(DateInterval.Day, Now(), MyDate).ToString



            Hour.Text = Hours

            Minutes.Text = Mins

            Seconds.Text = Secs

            Day.Text = Days


        End Sub


        Private Sub Minutes_Click(sender As Object, e As EventArgs) Handles Minutes.Click

        End Sub

        Private Sub Day_Click(sender As Object, e As EventArgs) Handles Day.Click, Day.Click

        End Sub

        Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged

        End Sub

        Private Sub Hour_Click(sender As Object, e As EventArgs) Handles Hour.Click, Hour.Click

        End Sub

        Private Sub Seconds_Click(sender As Object, e As EventArgs) Handles Seconds.Click

        End Sub
    End Class

    Saturday, November 2, 2019 6:07 AM

All replies

  • Once you subtract the two dates

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.subtract?redirectedfrom=MSDN&view=netframework-4.8#overloads


    you can just use the built in methods for each value you want ie days hours minutes seconds.

    https://docs.microsoft.com/en-us/dotnet/api/system.timespan.minutes?redirectedfrom=MSDN&view=netframework-4.8#System_TimeSpan_Minutes

    PS Use the Insert Code Block button at the top of the editor to make a nice formatted block of code when you post.

    Saturday, November 2, 2019 8:02 AM
  • Hello,

    The following code will countdown to the end of this year.

    Public Class Form1
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Dim timer1 = New Timer With {.Interval = 500}
            AddHandler timer1.Tick, AddressOf t_Tick
            Dim ts As TimeSpan = endTime.Subtract(Now)
            Label1.Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'")
            timer1.Start()
        End Sub
        Private endTime As New DateTime(2019, 12, 31, 0, 0, 0)
        Private Sub t_Tick(sender As Object, e As EventArgs)
            Dim ts As TimeSpan = endTime.Subtract(DateTime.Now)
            Label1.Text = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'")
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, November 2, 2019 9:56 AM
    Moderator
  • Hi,

    Kareninstructor provides a great solution, in addition, you can also achieve calculate years, months and days by the following code.

    Imports System.Text
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Start()
            Timer1.Interval = 10
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim observeTime As DateTime = DateTime.Parse("2020-1-1 0:0:0")
            Dim now As DateTime = DateTime.Now
            Dim ts As TimeSpan = observeTime.Subtract(now)
            Dim result As StringBuilder = New StringBuilder()
    
            'Calculate the number of years, months, days saparately
            Dim year As Integer = observeTime.Year - now.Year
            Dim month As Integer = observeTime.Month - now.Month
            Dim day As Integer = observeTime.Day - now.Day
            Dim hmh As Integer = (observeTime.Hour - now.Hour) * 3600 + (observeTime.Minute - now.Minute) * 60 + (observeTime.Second - now.Second)
    
            If hmh <= 0 Then
                day -= 1
                If (day <= 0 AndAlso month > 0) OrElse (day <= 0 AndAlso year > 0) Then
                    day = GetDay(now.Year, now.Month) - now.Day + observeTime.Day
                    month -= 1
    
                    If month < 0 AndAlso year > 0 Then
                        month += 12
                        year -= 1
                    End If
                End If
            End If
    
            If (day < 0 AndAlso month > 0) OrElse (day < 0 AndAlso year > 0) Then
                day = GetDay(now.Year, now.Month) - now.Day + observeTime.Day
                month -= 1
    
                If month < 0 AndAlso year > 0 Then
                    month += 12
                    year -= 1
                End If
            End If
    
            If month < 0 AndAlso year > 0 Then
                month += 12
                year -= 1
            End If
    
            'Exceed scheduled time
            If year < 0 OrElse (year = 0 AndAlso month < 0) OrElse (year = 0 AndAlso month = 0 AndAlso day < 0) Then
                lblShow.Text = "The scheduled time has been exceeded!"
                Timer1.Enabled = False
                Return
            End If
    
            'Years
            If year >= 0 Then
                result.Append(year & "Years ")
            End If
    
            'Months
            If month >= 0 Then
                result.Append(month & "Months ")
            End If
    
            'Days
            If day >= 0 Then
                result.Append(day & "Days ")
            End If
    
            'Calculate the number of days, hours, minutes, seconds using TimeSpan
            If ts.Hours >= 0 Then
                result.Append(ts.Hours & "Hours ")
            End If
    
            If ts.Minutes >= 0 Then
                result.Append(ts.Minutes & "Minutes ")
            End If
    
            If ts.Seconds >= 0 Then
                result.Append(ts.Seconds & "Seconds ")
            End If
    
            'Arrive scheduled time
            If result.Length = 0 Then
                result.Append("The scheduled time has arrived!")
                Timer1.Enabled = False
            End If
    
            'Show countdown on lblShow
            lblShow.Text = result.ToString()
        End Sub
    
        Private Function GetDay(ByVal year As Integer, ByVal month As Integer) As Integer
            Dim result As Integer = 31
    
            Select Case month
                Case 4, 6, 9, 11
                    result = 30
                Case 2
                    If (year Mod 100 <> 0) AndAlso (year Mod 4 = 0) OrElse (year Mod 400 = 0) Then
                        result = 29
                    Else
                        result = 28
                    End If
            End Select
    
            Return result
        End Function
    End Class
    

    Hope it be helpful!

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 4, 2019 9:48 AM
    Moderator
  • Here is another example using basic math. Currently set to countdown to Christmas.

    'countdown to date 
    'check https://www.xmasclock.com/
    
    Public Class DateCountdown
        Private WithEvents Timer1 As New System.Windows.Forms.Timer With {.Interval = 1000, .Enabled = True}
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Text = "Countdown!"
            ClientSize = New Size(400, 200)
            DoubleBuffered = True
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Invalidate()
        End Sub
    
        Private Sub Form5_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            'use date subtract for count difference
            Dim tNow As Date = Now
            Dim tLater As Date = #12/25/2019#
            Dim dys As Double = tLater.Subtract(tNow).TotalDays
    
            'parse the time difference format to day hr min sec using math
            Dim dy, hr, min, sec As Integer
            dy = CInt(Math.Floor(dys))                  'days only
            Dim hrs As Double = 24 * (dys - dy)         'hr + fraction
            hr = CInt(Math.Floor(hrs))                  'hours only
            Dim mins As Double = 60 * (hrs - hr)
            min = CInt(Math.Floor(mins))
            sec = CInt(Math.Floor(60 * (mins - min)))
    
            'draw the result
            With e.Graphics
                Dim base As Integer = ClientSize.Width / 8
    
                Using f As New Font(Font.FontFamily, CSng(0.2 * base), FontStyle.Bold),
                        p1 As New Pen(Color.Black, 2)
    
                    .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                    .Clear(Color.AntiqueWhite)
                    .FillRectangle(New SolidBrush(Color.FromArgb(150, Color.Maroon)), ClientRectangle)
    
                    .DrawString("Current Time: " & tNow.ToString & vbLf &
                                "Target Time: " & tLater.ToString, f, Brushes.AntiqueWhite, 1.5 * base, 0.5 * base)
    
                    DrawMyPie(e.Graphics, New Rectangle(base, 1.6 * base, base, base),
                              CInt(360 * dy / 365), "Days:" & vbLf & "  " & dy)
    
                    DrawMyPie(e.Graphics, New Rectangle(2.5 * base, 1.6 * base, base, base),
                              CInt(360 * hr / 24), "Hours:" & vbLf & "   " & hr.ToString)
    
                    DrawMyPie(e.Graphics, New Rectangle(4 * base, 1.6 * base, base, base),
                              CInt(360 * min / 60), "Mins:" & vbLf & "   " & min.ToString)
    
                    DrawMyPie(e.Graphics, New Rectangle(5.5 * base, 1.6 * base, base, base),
                              CInt(360 * sec / 60), "Secs:" & vbLf & "   " & sec.ToString)
                End Using
            End With
        End Sub
    
        Private Sub DrawMyPie(g As Graphics, rect As Rectangle, ang As Single, txt As String)
            With g
                .FillEllipse(Brushes.Maroon, rect)
                .DrawEllipse(Pens.Black, rect)
                .FillPie(Brushes.LightGoldenrodYellow, rect, -90, ang)
                .DrawString(txt, New Font(Font.FontFamily, CSng(0.2 * rect.Height), FontStyle.Bold), Brushes.AntiqueWhite,
                            rect.X + (0.2 * rect.Height), rect.Bottom + 10)
            End With
        End Sub
    
        Private Sub Form5_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Invalidate()
        End Sub
    End Class

    Monday, November 4, 2019 11:01 AM