Converts from a timestamp to a DateTime. The result is in local time. - CSharp System

CSharp examples for System:DateTime Timestamp

Description

Converts from a timestamp to a DateTime. The result is in local time.

Demo Code


using System;/*ww w. jav a2  s  .c om*/

public class Main{
        /// <summary>
        /// Converts from a timestamp to a <see cref="DateTime"/>. The result is in local time.
        /// </summary>
        /// <param name="timestamp">
        /// The timestamp.
        /// </param>
        /// <returns>
        /// The equivalent <see cref="DateTime"/> in local time.
        /// </returns>
		public static DateTime FromTimestamp(Double timestamp)
        {
            try
            {
                DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                return origin.AddSeconds(timestamp).ToLocalTime();
            }
            catch
            {
                return DateTime.Now;
            }
        }
        /// <summary>
        /// Converts from a timestamp to a <see cref="DateTime"/>. The result is in local time.
        /// </summary>
        /// <param name="timestamp">
        /// The timestamp.
        /// </param>
        /// <returns>
        /// The equivalent <see cref="DateTime"/> in local time.
        /// </returns>
		public static DateTime FromTimestamp(Int64 timestamp)
        {
            try
            {
                return FromTimestamp((double)timestamp);
            }
            catch
            {
                return DateTime.Now;
            }
        }
}

Related Tutorials