http://www.cnblogs.com/qingliuyu/p/3835858.html
以下是C#下的日期与unix时间戳的相互转换:
/// <summary> /// 日期转换成unix时间戳 /// </summary> /// <param name="dateTime"></param> /// <returns></returns> public static long DateTimeToUnixTimestamp(DateTime dateTime) { var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind); return Convert.ToInt64((dateTime - start).TotalSeconds); } /// <summary> /// unix时间戳转换成日期 /// </summary> /// <param name="unixTimeStamp">时间戳(秒)</param> /// <returns></returns> public static DateTime UnixTimestampToDateTime(this DateTime target, long timestamp) { var start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind); return start.AddSeconds(timestamp); }
/// <summary> /// unix时间戳转换成日期 /// </summary> /// <param name="unixTimeStamp">时间戳(秒)</param> /// <returns></returns> public static DateTime UnixTimestampToDateTime(long timestamp) { var start = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified); return start.AddMilliseconds(timestamp); }
学习交流群:364976091