C# 时间与时间戳互转 13位
2021-06-28 01:06
标签:timezone tst tick rtt localtime param new t start sum /// C# 时间与时间戳互转 13位 标签:timezone tst tick rtt localtime param new t start sum 原文地址:https://www.cnblogs.com/KyrieCC/p/10060022.html
/// 将c# DateTime时间格式转换为Unix时间戳格式
///
/// 时间
///
public static long ConvertDateTimeToInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
long t = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位
return t;
}
///
/// 时间戳转为C#格式时间
///
///
///
private DateTime ConvertStringToDateTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000");
TimeSpan toNow = new TimeSpan(lTime);
return dtStart.Add(toNow);
}