Converts a DateTime to seconds since the Unix Epoch (1st of January 1970 00:00:00 GMT) - CSharp System

CSharp examples for System:DateTime Second

Description

Converts a DateTime to seconds since the Unix Epoch (1st of January 1970 00:00:00 GMT)

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//  w  w  w  .  j a  va 2  s  .c  om

public class Main{
        /// <summary>
        /// Converts a DateTime to seconds since the Unix Epoch (1st of January 1970 00:00:00 GMT)
        /// </summary>
        /// <param name="dateTime">The DateTime to convert</param>
        /// <returns></returns>
        public static long DateTimeToUnixTime(DateTime dateTime)
        {
            DateTime date_time_base = new DateTime(1970, 1, 1, 0, 0, 0, 0);

            TimeSpan span = dateTime.ToUniversalTime() - date_time_base;

            return (long)span.TotalSeconds;
        }
}

Related Tutorials