C# 时间戳的相关操作
2024-09-26 21:52:35
一般的 时间戳 格式分为两种 即 10位(秒)时间戳 与 13位(毫秒)时间戳
时间戳 类型也分为两种 即 本地时间戳 与 世界统一(UTC)时间戳
废话不多说,直接上代码:
一、时间戳获取方法
/// <summary>
/// 获取时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc)</param>
/// <param name="format">时间戳格式(只能为 10、13)</param>
/// <returns></returns>
private double GetTimestamp(int format, DateTimeKind timeKind)
{
TimeSpan timeSpan = new TimeSpan(); switch (timeKind)
{
case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(, , , , , , timeKind); break;
case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(, , , , , , timeKind); break;
default: throw new Exception("时间类型 只能为 Local、Utc");
} switch (format)
{
case : return timeSpan.TotalSeconds;
case : return timeSpan.TotalMilliseconds;
default: throw new Exception("时间戳格式 只能为 10、13");
}
} /// <summary>
/// 获取10位时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public int Get10Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
{
return Convert.ToInt32(GetTimestamp(, timeKind));
} /// <summary>
/// 获取13位时间戳
/// </summary>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public long Get13Timestamp(DateTimeKind timeKind = DateTimeKind.Local)
{
return Convert.ToInt64(GetTimestamp(, timeKind));
}
二、时间戳验证方法
/// <summary>
/// 验证时间戳(10位、13位皆可)
/// </summary>
/// <param name="timestamp">时间戳</param>
/// <param name="timeDiff">允许时差(10位时单位为 秒,13位时单位为 毫秒)</param>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <returns></returns>
public bool ValidateTimestamp(double timestamp, int timeDiff, DateTimeKind timeKind = DateTimeKind.Local)
{
TimeSpan timeSpan = new TimeSpan(); switch (timeKind)
{
case DateTimeKind.Utc: timeSpan = DateTime.UtcNow - new DateTime(, , , , , , timeKind); break;
case DateTimeKind.Local: timeSpan = DateTime.Now - new DateTime(, , , , , , timeKind); break;
default: throw new Exception("时间类型 只能为 Local、Utc");
} double nowTimestamp = ; //现在的时间戳
int format = timestamp.ToString("f0").Length; switch (format)
{
case : nowTimestamp = timeSpan.TotalSeconds; break;
case : nowTimestamp = timeSpan.TotalMilliseconds; break;
default: throw new Exception("时间戳格式 错误");
} double nowTimeDiff = nowTimestamp - timestamp; //现在的时差 if (-timeDiff <= nowTimeDiff && nowTimeDiff <= timeDiff)
return true;
else
return false;
}
三、由 时间戳 转换为 DateTime 方法
/// <summary>
/// 将时间戳装换为DateTime(10位、13位皆可)
/// </summary>
/// <param name="timestamp">时间戳</param>
/// <param name="timeKind">时间类型(只能为 Local、Utc,默认 Local)</param>
/// <param name="toTimeKind">返回的时间类型(只能为 Local、Utc,默认与 timeKind 一致)</param>
/// <returns></returns>
public DateTime TimestampToDateTime(double timestamp, DateTimeKind timeKind = DateTimeKind.Local, DateTimeKind toTimeKind = DateTimeKind.Unspecified)
{
DateTime startTime;
toTimeKind = timeKind; switch (timeKind)
{
case DateTimeKind.Utc: startTime = new DateTime(, , , , , , timeKind); break;
case DateTimeKind.Local: startTime = new DateTime(, , , , , , timeKind); break;
default: throw new Exception("时间类型 只能为 Local、Utc");
} DateTime newTime;
int format = timestamp.ToString("f0").Length; switch (format)
{
case : newTime = startTime.AddSeconds(timestamp); break;
case : newTime = startTime.AddMilliseconds(timestamp); break;
default: throw new Exception("时间戳格式 错误");
} if (newTime.Kind != toTimeKind)
newTime = toTimeKind == DateTimeKind.Local ? newTime.ToLocalTime() : newTime.ToUniversalTime(); return newTime;
}
最新文章
- AKKA 笔记 - 有限状态机 -2
- js获取域名
- Angular2 起步(1)
- VUE2.0不可忽视的很多变化
- SQL关于分页的sql查询语句 limit 和row_number() OVER函数
- JAVA通过HTTP访问:Post+Get方式(转)
- Java Hour 25 Packages
- IS_POST:判断是否存在POST提交
- c++实现类似Common Lisp的多参数加法和比较
- 将ecshop中的session机制重写,从DB移植到Memcache中去
- iOS,长按图片保存实现方法,轻松搞定!
- activiti笔记二:用户任务
- POJ2352_Stars(段树/单点更新)
- Java模拟新浪微博登陆抓取数据
- 利用GPU实现无尽草地的实时渲染
- jzoj3086 [分層圖最短路]
- Vue2.5开发去哪儿网App 城市列表开发之 Vuex实现数据共享及高级使用
- Beta 完结撒花 —— 事后诸葛亮
- oracle 导入导出指定表
- Python——Shell编程关于Sha-Bang(#!)
热门文章
- unity_小功能实现(敌人追踪主角)
- git 如何实现进行多人协作开发(远程仓库)
- CF EDU 1101D GCD Counting 树形DP + 质因子分解
- [普及]NOIP 2015 推销员 贪心
- CF1005E1 Median on Segments (Permutations Edition) 思维
- Linux音频编程(二)声卡介绍
- MySQL性能调优与架构设计(简朝阳)
- RHEL7破解密码操作步骤
- python每日经典算法题5(基础题)+1(中难题)
- 【LeetCode】524-通过删除字母匹配到字典里最长单词