Gant Software Systems

A Simple Countdown Timer

We’re about to move and I’ll be out of pocket for several days as we cart all our stuff to the new house. In the meantime, my six year old keeps asking “how much longer until we move, daddy?”. I finally decided that I would make a simple little timer to not only show how long until we start moving, but several other key events of the move as well because I find myself counting down the days as well (yeah, I really want out of this house). So I did and I’m going to put the code here. There’s nothing earth shattering here – it’s my typical “Stupid Coding Tricks” blog post. The whole point of these sorts of things is to show how a simple little program that can be written in a few minutes can still be useful. I decided to build this little app in winforms because it was the simplest thing that could possibly work.

First, I considered what I wanted to track. I came up with four things:

  1. The time remaining until we signed the papers selling our current house (Freedom, to be sold on 9/25/2014 at 9:30 AM)
  2. The time remaining until we signed the papers purchasing our new house (Wabash, to be signed on 9/25/2014 at noon, an hour’s drive away from where we signed the freedom papers)
  3. The time remaining until the professional movers showed up (9/26/2014 9:30 AM)
  4. The time remaining until we contractually have to be out of the old house (we’re getting out well beforehand)
    With these things in mind, I build the following form in winforms. The controls shown below are all labels.

Countdown Timer Layout

With that done, all I needed to do was calculate the time remaining to each of the above events.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public partial class Form1 : Form
{
private DateTime FreedomSigning = new DateTime(2014,9,25,9,0,0);
private DateTime WabashSigning = new DateTime(2014,9,25,12,0,0);
private DateTime MoveStarts = new DateTime(2014,9,26,9,15,0);
private DateTime FreedomHandoff = new DateTime(2014,9,27,17,0,0);
private Timer Timer = new Timer();
private string timeFormat = "{0:dd} days and {0:hh\\:mm\\:ss}";

public Form1()
{

InitializeComponent();
Timer.Interval = 1000;
Timer.Tick += Timer_Tick;
Timer.Enabled = true;
}

void Timer_Tick(object sender, EventArgs e)
{

var now = DateTime.Now;
var fsDiff = FreedomSigning - now;
var wbDiff = WabashSigning - now;
var msDiff = MoveStarts - now;
var fhDiff = FreedomHandoff - now;

lblFreedomSigningRemaining.Text = string.Format(timeFormat, fsDiff);
lblWabashSigningRemaining.Text = string.Format(timeFormat, wbDiff);
lblMoveStartsRemaining.Text = string.Format(timeFormat, msDiff);
lblFreedomHandoffRemaining.Text = string.Format(timeFormat, fhDiff);
}
}

At the top, I’m simply declaring DateTime constants for the various events. I’m also declaring a Timer object that will be used to update the display every second. I’m also declaring the time format string once, because I’m lazy and don’t wish to type repetitively.

In the form’s constructor, after the InitializeComponent call, I’m setting the interval of the timer and then giving it a callback to call when it fires (Timer_Tick) before setting it enabled.

When the timer fires, I get the current time into the “now” variable and then quickly calculate the difference in times for each of the events. Then I set the labels on the form, using the format string I set up above. When it runs, the UI looks like the image below (but it obviously updates once per second).

Countdown Timer Running

Obviously this is pretty much the simplest program I’ve ever shown on this site. But that’s really the point of the whole thing. Very small, uncomplicated programs can be extremely helpful at times. This is why I think it behooves nearly everyone to learn basic programming skills – you can build small, useful projects that provide value for you without having to be perfectly architected by someone charging three figures an hour.