我前段时间写的2个方法 觉得还不错 共享给大家
/// <summary>
/// 将系统时间转换成UNIX时间戳(把系统时间转换成Mysql时间)
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public long GetNowPHPTick(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return (long)(intResult - 8 * 60 * 60);
}
/// <summary>
/// 将UNIX时间戳转换成系统时间(Mysql取出来的转换成系统时间)
/// </summary>
/// <param name="dateDouble"></param>
/// <returns></returns>
public DateTime GetDateTime(long dateDouble)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(dateDouble + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}
|