How do you convert epoch time in C#?

后端 未结 14 2327
野的像风
野的像风 2020-11-22 06:51

How do you convert Unix epoch time into real time in C#? (Epoch beginning 1/1/1970)

14条回答
  •  攒了一身酷
    2020-11-22 07:24

    I use following extension methods for epoch conversion

    public static int GetEpochSeconds(this DateTime date)
        {
            TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
            return (int)t.TotalSeconds;
        }
    
    public static DateTime FromEpochSeconds(this DateTime date, long EpochSeconds)
        {
            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            return epoch.AddSeconds(EpochSeconds);
    
        }
    

提交回复
热议问题