当前位置:网站首页>C# .net 时间戳和时间转换 支持时区
C# .net 时间戳和时间转换 支持时区
2022-07-26 18:43:00 【应用猎人】
/// <summary>
/// 本时区日期时间转时间戳
/// </summary>
/// <param name="datetime">时间</param>
/// <param name="toMilliseconds">是否精确到毫秒</param>
/// <returns></returns>
public static long ToUnixTimeStamp(this DateTime datetime, bool toMilliseconds = true)
{
DateTime beginTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);//标准时间
DateTime timeUTC = datetime.ToUniversalTime();//转成标准时间
TimeSpan ts = (timeUTC - beginTimeUtc);//当前标准时间减去标准起始时间
if (toMilliseconds)
{
return (Int64)ts.TotalMilliseconds;//精确到毫秒
}
return (Int64)ts.TotalSeconds;//精确到秒
}
/// <summary>
/// 时间戳转本时区日期时间
/// </summary>
/// <param name="TimeStamp"></param>
/// <param name="toMilliseconds">是否精确到毫秒</param>
/// <returns></returns>
public static DateTime ToDateTime( this long TimeStamp, bool toMilliseconds = true)
{
DateTime startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);//标准时间
if (toMilliseconds)
{
return startTime.AddTicks(TimeStamp * 10000).ToLocalTime();//标准时间转本地时间
}
else
{
return startTime.AddTicks(TimeStamp * 10000000).ToLocalTime();//标准时间转本地时间
}
}
边栏推荐
- 2022牛客多校联赛第三场 题解
- [PHP] save session data to redis
- Win11 U盘驱动异常怎么调整为正常?
- DDL,DQL,DML语句
- Save 50% of the cost JD cloud releases a new generation of hybrid CDN products
- Three paradigms of database design
- If the key is forgotten and multiple devices have different keys, how does the cloud synchronize
- Is qiniu a channel for securities companies? Is it safe to open an account?
- The authentication type 10 is not supported
- Redis介绍
猜你喜欢
随机推荐
Test interview question set UI automated test
2022/07/26 学习笔记 (day16) 抽象与接口
[MySQL must know and know] log details
SVN - 详细文档
Do you know the difference between safety test, functional test and penetration test?
"Weilai Cup" 2022 Niuke summer multi school training camp 2
Pads draw 2.54mm row needle
视频直播源码,实现上下滚动的广告效果
Still using xshell? You are out. I recommend a more modern terminal connection tool
使用三重损失和孪生神经网络训练大型类目的嵌入表示
Image preview embedding location of blog maintenance record
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
Redis6
After working for 13 years, I have a little software testing experience and feelings
LeetCode每日一练 —— 189. 轮转数组
3r平衡型理财产品会有风险吗?风险大吗?
博客维护记录之图片预览嵌入位置问题
中天钢铁在 GPS、 AIS 调度中使用 TDengine
Will 3R balanced financial products have risks? Is it risky?
Uiobject2 of uiautomator2 common classes









