分享在C#中时间戳是怎么转换的?

分享在C#中时间戳是怎么转换的?,第1张

分享在C#中时间戳是怎么转换的? 时间戳转DateTime

timestamp为10位秒级* 10000000,若为13位毫秒级*10000。

private DateTime TimestampToDateTime(long timestamp)

{

DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));

long lTime = timestamp * 10000000;

TimeSpan nowTimeSpan = new TimeSpan(lTime);

DateTime resultDateTime = dateTimeStart.Add(nowTimeSpan);

return resultDateTime;

}

DateTime转时间戳

秒级

private int DateTimeToTimestamp(DateTime time)
{
DateTime startDateTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(197, 1, 1));
return Convert.ToInt32((time - startDateTime).TotalSeconds);

}

以上就是分享在C#中时间戳是怎么转换的?的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/684852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存