December 16th, 2009
Posted in C#
I’ve got an exam application that has a fixed time limit and relays the clock winding down to the test taker via an AJAX call. So we know what time it is now (DateTime.Now), we know when we started the exam (set value in session) and we know the time limit of the exam (pulled from the database). How do we bring it all together? We have to figure out our finish time by running the AddMinutes method against our exam beginning DateTime value, adding our predefined number of minutes set in the time limit. Next, we simply subtract our begin time from our finish time, and that leaves us with a TimeSpan value. The only thing remaining is to do something with that TimeSpan value. It has a host of methods for conversion, but we are going to run the TotalMinutes method this go around to get us the countdown value. Maybe not as simple as VB’s datediff, but simple enough.