Converts from a DateTime to a timestamp. - CSharp System

CSharp examples for System:DateTime Timestamp

Description

Converts from a DateTime to a timestamp.

Demo Code


using System;/*from   w  w  w.  j a  va 2s. c om*/

public class Main{
        /// <summary>
        /// Converts from a <see cref="DateTime"/> to a timestamp.
        /// </summary>
        /// <param name="date">
        /// The <see cref="DateTime"/>.
        /// </param>
        /// <returns>
        /// The timestamp.
        /// </returns>
		public static double ToTimestamp(DateTime date)
        {
            try
            {
                DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                TimeSpan diff = date.ToUniversalTime() - origin;
                return Math.Floor(diff.TotalSeconds);
            }
            catch
            {
                return 0;
            }
        }
}

Related Tutorials