5 cent hint – Unix timestamp to c# datetime

By | 21. March 2017

If you need to covert Unix timestamp  to DateTime then here it is:

 

public DateTime TimestampToDatetime(long timeStamp)
{
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddSeconds(timeStamp);
}

var date = TimestampToDatetime(1490091991);

Update:

DateTime to Unix timestamp

     public static int DateToStamp(DateTime datetime)
        {
           var date = new DateTime(1970, 1, 1, 0, 0, 0, datetime.Kind);
           var unixTimestamp = System.Convert.ToInt32((datetime - date).TotalSeconds);
           return unixTimestamp;
        }

 

Leave a Reply