Convert Unix timestamp to datetime - CSharp System

CSharp examples for System:DateTime Timestamp

Description

Convert Unix timestamp to datetime

Demo Code


using System;// w w w . j a  v  a2  s  .c o  m

public class Main{
        /// <summary>
        /// Convert Unix timestamp to date/time
        /// </summary>
        /// <param name="unixTimeStamp">Unix timestamp</param>
        /// <returns>DateTime representation of Unix timestamp</returns>
        public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            try
            {
                // Unix timestamp is seconds past epoch
                DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
                return dtDateTime;
            }
            catch
            {
                return DateTime.Now;
            }
        }
}

Related Tutorials