Convert a datetime to its corresponding unix timestamp value. - CSharp System

CSharp examples for System:DateTime Unix

Description

Convert a datetime to its corresponding unix timestamp value.

Demo Code


using System;//ww  w.  j av  a 2 s  .c  om

public class Main{
        /// <summary>
        /// Convert a datetime to its corresponding unix timestamp value.
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static long ToUnixTime(DateTime date)
        {
            DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan timespan = date - unixStart;
            return (long)Math.Floor(timespan.TotalSeconds);
        }
}

Related Tutorials