NET4.6下的UTC时间转换

int UTCSecond = (int)((DateTimeOffset)DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local)).ToUnixTimeSeconds(); this

DateTime time = DateTimeOffset.FromUnixTimeSeconds(UTCSecond).DateTime; spa

Console.WriteLine("当前utc:{0},秒数:{1},换算时间:{2}", DateTimeOffset.Now, UTCSecond, time.ToString("F")); blog

UTCSecond = Q.Helper.TimeHelper.ToUTCSecond(DateTime.Now); ci

time = DateTimeOffset.FromUnixTimeSeconds(UTCSecond).DateTime; it

Console.WriteLine("当前utc:{0},秒数:{1},换算时间:{2}", DateTimeOffset.Now, UTCSecond, time.ToString("F")); table

Console.Read();原理

 

 

 

QCOMMON请使用NUGET包管理器搜索安装 QCommon date

基本原理以下 搜索

 

/// <summary> rsa

/// UTC时间

/// </summary>

static readonly DateTime UTC = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Utc);

/// <summary>

/// 时间转UTC秒

/// </summary>

/// <param name="dateTime">时间(当前时区)</param>

/// <returns>UTC秒</returns>

public static int ToUTCSecond(this DateTime dateTime)

{

return (int)dateTime.ToUniversalTime().Subtract(UTC).TotalSeconds;

}

 

/// <summary>

/// UTC秒转当地时间

/// </summary>

/// <param name="second">秒数</param>

/// <returns>当时时间</returns>

public static DateTime ToDateTime(this int second)

{

return UTC.AddSeconds(second).ToLocalTime();

}

相关文章
相关标签/搜索