Dirk Bergmann

Developer, panographer. Does consultancy work from own company. Lives in Chiang Mai, Thailand.


Chiang Mai

Hua Hin

Thailand Hotels

ListVote - List. Vote. Learn.

Partnerseek - Find Partners

Seconds Passed since Point in Time

After not finding a simple app online calculating the seconds since a past point in time, I made this little web form.

It uses the rather obscure DateTime.ParseExact () function which provides an unforgiving way to parse a date string according to a specific date format string. The date string has to match the format string exactly, otherwise an exception will be thrown. Also, don't forget to escape slashes in your date format string.

Here's some sample code:

    string theDate = tbDateInput.Text;
                System.IFormatProvider format =
                string expectedformat = "yyyyMMddHHmmss";
                DateTime startDate = DateTime.ParseExact(theDate,
                    expectedformat, format, System.Globalization.
                TimeSpan sp = DateTime.Now - startDate;
                lblResult.Text = String.Format("{0} seconds have passed since {1}",
                    startDate.ToLongDateString () + " " +
                startDate.ToLongTimeString ());