Dirk Dot Net

Creator of useful things. Based in Chiang Mai, Northern Thailand.

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 =
                    System.Globalization.CultureInfo.InvariantCulture;
    
                string expectedformat = "yyyyMMddHHmmss";
    
                DateTime startDate = DateTime.ParseExact(theDate,
                    expectedformat, format, System.Globalization.
                    DateTimeStyles.AllowWhiteSpaces
                    );
    
    
                TimeSpan sp = DateTime.Now - startDate;
                lblResult.Text = String.Format("{0} seconds have passed since {1}",
                    sp.TotalSeconds,
                    startDate.ToLongDateString () + " " +
                startDate.ToLongTimeString ());