Unity时间格式和时间戳转换
(1)获取当前时间转化为时间戳
System.TimeSpan st = System.DateTime.UtcNow - new System.DateTime(1970, 1, 1, 0, 0, 0);
Debug.Log(Convert.ToInt64(st.TotalSeconds));
(2)把时间戳转化为日期时间
System.DateTime startTime = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
System.DateTime dt = startTime.AddSeconds(1558340395);
string t = dt.ToString("yyyy/MM/dd HH:mm:ss");
Debug.Log(t);
(3)C#时间字符串格式转时间戳
public long DateTime2TimeStamp_String(string now, bool getSecond = true)
{
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0);
TimeSpan ts = Convert.ToDateTime(now) - startTime;
return getSecond
? (long)ts.TotalSeconds
: (long)ts.TotalMilliseconds;
}
(4)将string类型的时间戳 转换成string类型的时间
private string GetDateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime);
DateTime targetDt = dtStart.Add(toNow);
return targetDt.ToString("yyyy/MM/dd");
}