/// <summary>
/// 時間戳反轉為時間,有很多中翻轉方法,但是,請不要使用過字符串(string)進行操作,大家都知道字符串會很慢!
/// </summary>
/// <param name="TimeStamp">時間戳</param>
/// <param name="AccurateToMilliseconds">是否精確到毫秒</param>
/// <returns>返回一個日期時間</returns>
public static DateTime GetTime(long TimeStamp, bool AccurateToMilliseconds = false)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
if (AccurateToMilliseconds)
{
return startTime.AddTicks(TimeStamp * 10000);
}
else
{
return startTime.AddTicks(TimeStamp * 10000000);
}
}
舉例:
string timestr = "1645517610";
DateTime time = Convert.ToDateTime(GetTime(long.Parse(timestr)));
日期與時間戳互轉參考地址:
https://blog.csdn.net/u013899802/article/details/120706554
