我有两个函数可以在.NET DateTime和Unix时间戳之间进行转换。但我不确定我是否考虑了时区(EPOCH和DateTime参数)。请帮忙:
public class Util
{
static DateTime EPOCH = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
public static DateTime ConvertUnixTimeStamp(ulong unixTimeStamp)
{
var dt = EPOCH.AddSeconds(unixTimeStamp);
return dt;
}
public static double ConvertDatetimeToUnixTimeStamp(DateTime dt)
{
TimeSpan t = (dt - EPOCH);
return Math.Floor(t.TotalSeconds);
}
}
答案 0 :(得分:0)
static DateTime EPOCH = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
public static double ConvertDatetimeToUnixTimeStamp(DateTime date , int Time_Zone = 0)
{
TimeSpan The_Date = (date - EPOCH);
return Math.Floor(The_Date.TotalSeconds) - (Time_Zone * 3600);
}