Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to make a countdown timer to a set date in Visual Basic 2010?

I want the application to continue counting toward that date every time I open it.

I also want it to show days, hours, minutes and seconds along with the progress bar if possible.

Thank you

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    In addition to the Date class in VB, there is also a TimeSpan class that tells you a lot about the difference between two dates.

    To display the time remaining, you need an event time:

    Dim eventDate As New DateTime(2010, 12, 25) ' Xmas

    To find out how long before Christmas, subtract eventDate from current date and store it in a TimeSpan:

    Dim ts As New TimeSpan

    ts = eventDate - Date.Now

    A TimeSpan has fields with Days, Hours, Minutes, Seconds etc. So you can display those:

    tb1.Text = ts.Days.ToString() ' Days remaining

    tb2.Text = ts.Hours.ToString() ' Hours remaining

    etc....

    For a progress bar, you need 3 dates. Some start date, the current date, and the end date. (You can't do a progress bar with only 2 dates...think about it ;-)

    If you have a start date, then you can compute how far along you are using the Ticks field. Compute total ticks between start date and now. Divide by Ticks between end date and start date. Multiply by 100 and that will give you the % of the way there, which you can map to a progress bar. Something like this(off the top of my head):

    Dim d As Double

    d = 100 * (Date.Now - startDate).Ticks / (eventDate - startDate).Ticks

    You can update your time remaining and progress bar from a timer event if this is a Win app. For a web app you just do it on every page refresh (and use Javascript to keep it running in between?)

    Don't forget to handle the case that Date.Now > eventDate. Otherwise you'll end up displaying that there are -1 days left until Christmas :-(

  • 5 years ago

    Countdown Timer To Date

Still have questions? Get your answers by asking now.