Dirk Bergmann

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

Twitter

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 =
                    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 ());