Returns a unix Epoch time given a Date - CSharp System

CSharp examples for System:DateTime Unix

Description

Returns a unix Epoch time given a Date

Demo Code



public class Main{
        /// <summary>
        ///     Returns a unix Epoch time given a Date
        /// </summary>
        public static long ToEpochTime(this DateTime dt, bool toMilliseconds = false)
        {/*from  w  ww . j a v  a2  s.co m*/
            var seconds = (long) (dt - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
            return toMilliseconds ? seconds * 1000 : seconds;
        }
}

Related Tutorials