string localID = TimeZoneInfo.Local.Id;
Debug.Log(localID);
DateTime StartDateTime = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
DateTime StartDateTime1 = TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("Central Pacific Standard Time"));
Debug.Log("中国时间:"+StartDateTime);
Debug.Log("所罗门群岛,新喀里多尼亚"+StartDateTime1);
DateTime NowChain = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
DateTime NowCentral = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Central Pacific Standard Time"));
Debug.Log("中国时间——当前时间:" + NowChain);
Debug.Log("所罗门群岛,新喀里多尼亚——当前时间:" + NowCentral);
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
DateTime NowTime = TimeZone.CurrentTimeZone.ToLocalTime(DateTime.Now);
Debug.Log("TimeZone API 时区时间"+startTime);
Debug.Log("TimeZone API 当前 时区时间" + NowTime);
Debug.Log("时间戳 :"+ (NowChain - StartDateTime).TotalMilliseconds);
foreach (var item in TimeZoneInfo.GetSystemTimeZones()) // 获取所有时区ID
{
Debug.Log(item.Id);
}
总结:
TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
被
TimeZoneInfo.ConvertTime(new DateTime(1970, 1, 1), TimeZoneInfo.FindSystemTimeZoneById("China Standard Time"));
代替了
两者区别,老API( TimeZone)获取的是时区时间,例如中国北京在东八区,获取的时间会加8小时
新API(TimeZoneInfo)是以当前所在时区为原点(例如,在中国获取中国时间,为正常值。获取 所罗门群岛,新喀里多尼亚 时间,会加3小时) 注:个人理解, 不知道正确与否,不会设置当前地电脑的时区,如有错误,还望指正!
时间戳转化还是老样子,详见代码块
附一份扩展脚本: (注意事项,不可直接使用ID字符串,Windows和Linux的ID不一样,会查不到ID)
using System;
public static class TimestampExt
{
public static DateTime GetNowTime()
{
string ID = TimeZoneInfo.Local.Id;
DateTime NowTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById(ID));
return NowTime;
}
/// <summary>
/// 时间转时间戳
/// </summary>
/// <param name="_dataTime">时间</param>
/// <param name="MilliTime">毫秒计时</param>
/// <returns></returns>
public static long ToTimestamp(this DateTime _dataTime, bool Millisecond = true)
{
string ID = TimeZoneInfo.Local.Id;
DateTime start = new DateTime(1970, 1, 1) + TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
DateTime startTime = TimeZoneInfo.ConvertTime(start, TimeZoneInfo.FindSystemTimeZoneById(ID));
DateTime NowTime = TimeZoneInfo.ConvertTime(_dataTime, TimeZoneInfo.FindSystemTimeZoneById(ID));
long timeStamp;
if (Millisecond)
timeStamp = (long)(NowTime - startTime).TotalMilliseconds; // 相差毫秒数
else
timeStamp = (long)(NowTime - startTime).TotalSeconds; // 相差秒数
return timeStamp;
}
public static DateTime ToDateTime(this long stamp, bool Millisecond = true)
{
string ID = TimeZoneInfo.Local.Id;
DateTime start = new DateTime(1970, 1, 1) + TimeZoneInfo.Local.GetUtcOffset(DateTime.Now);
DateTime startTime = TimeZoneInfo.ConvertTime(start, TimeZoneInfo.FindSystemTimeZoneById(ID));
DateTime dt;
if (Millisecond)
dt = startTime.AddMilliseconds(stamp);
else
dt = startTime.AddSeconds(stamp);
Console.WriteLine(dt.ToString("yyyy/MM/dd HH:mm:ss:fff"));
return dt;
}
}
调用方法:
long timeStamp = System.DateTime.Now.ToTimestamp();
Debug.Log(timeStamp);
DateTime dateTime = timeStamp.ToDateTime();
Debug.Log(dateTime.ToString("yyyy/MM/dd HH:mm:ss:fff"));