Converts DateTime object to Epoch time. - CSharp System

CSharp examples for System:DateTime Convert

Description

Converts DateTime object to Epoch time.

Demo Code

// Copyright (c) Microsoft Corporation. All rights reserved. 
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from w  ww .j av  a2 s .c  om

public class Main{
        /// <summary>
        /// Converts DateTime object to Epoch time.
        /// </summary>
        /// <param name="dateTime">DateTime object.</param>
        /// <returns>Epoch time representation.</returns>
        public static ulong ConvertToEpochTime(this DateTime dateTime)
        {
            var date = dateTime.ToUniversalTime();
            var ts = date - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            return Convert.ToUInt64(ts.TotalSeconds);
        }
}

Related Tutorials