首页 > 技术文章 > UTC和时间相互转换

hornet 2014-10-29 14:12 原文

// ToLocalTime() UTC时间转换为本地时间
public static DateTime UtcToDateTime(long number)
{            
  return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(number).ToLocalTime();
}

//本地时间转换成UTC时间 C#和Unix时间戳的转换
private static long DateTimeToUtc( DateTime dt)
{
   return ( long)(dt.ToUniversalTime() - ( new DateTime(1970, 1, 1, 0, 0, 0, 0))).TotalSeconds;
}

推荐阅读