1.C# DateTime转换为Unix时间戳
1 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 2 long timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数 3 System.Console.WriteLine(timeStamp);
2.Unix时间戳转换为C# DateTime
1 long unixTimeStamp = 1478162177; 2 System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 3 DateTime dt = startTime.AddSeconds(unixTimeStamp); 4 System.Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:ffff"));