当前位置:网站首页>C# 时间戳与时间的互相转换
C# 时间戳与时间的互相转换
2022-07-30 11:30:00 【※※冰馨※※】
什么是时间戳?
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
时间戳在线转换网址:https://tool.lu/timestamp,时间戳的转换网址有很多,经常用的还有站长工具。
下附代码,在控制台中粘贴在启动类即可使用,需引用(using System)命名空间 ;
/// <summary>
/// 取时间戳,高并发情况下会有重复。想要解决这问题请使用sleep线程睡眠1毫秒。
/// </summary>
/// <param name="AccurateToMilliseconds">精确到毫秒</param>
/// <returns>返回一个长整数时间戳</returns>
public static long GetTimeStamp(bool AccurateToMilliseconds = false)
{
if (AccurateToMilliseconds)
{
// 使用当前时间计时周期数(636662920472315179)减去1970年01月01日计时周期数(621355968000000000)除去(删掉)后面4位计数(后四位计时单位小于毫秒,快到不要不要)再取整(去小数点)。
//备注:DateTime.Now.ToUniversalTime不能缩写成DateTime.Now.Ticks,会有好几个小时的误差。
//621355968000000000计算方法 long ticks = (new DateTime(1970, 1, 1, 8, 0, 0)).ToUniversalTime().Ticks;
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000;
}
else
{
//上面是精确到毫秒,需要在最后除去(10000),这里只精确到秒,只要在10000后面加三个0即可(1秒等于1000毫米)。
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
}
/// <summary>
/// 时间戳反转为时间,有很多中翻转方法,但是,请不要使用过字符串(string)进行操作,大家都知道字符串会很慢!
/// </summary>
/// <param name="TimeStamp">时间戳</param>
/// <param name="AccurateToMilliseconds">是否精确到毫秒</param>
/// <returns>返回一个日期时间</returns>
public static DateTime GetTime(long TimeStamp, bool AccurateToMilliseconds = false)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
if (AccurateToMilliseconds)
{
return startTime.AddTicks(TimeStamp * 10000);
}
else
{
return startTime.AddTicks(TimeStamp * 10000000);
}
}边栏推荐
- Kubernetes 入门实战03 中级篇
- Interviewer: Redis bloom filter and the cuckoo in the filter, how much do you know?
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- 嵌入式环境下并发控制与线程安全
- Typroa 替代工具marktext
- 概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
- Concepts of cloud-native applications and 15 characteristics of cloud-native applications
- 明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级
- The method of judging the same variable without the if branch
- Verilog语法基础HDL Bits训练 07
猜你喜欢

win下怎么搭建php环境的方法教程

Redis 主从复制

C语言 — 位运算操作

SCM engineers written questions induction summary

Summary of text alignment, line height, space, etc.

面试官:Redis中的布隆过滤器与布谷鸟过滤器,你了解多少?

【32. 图中的层次(图的广度优先遍历)】

Horizontal comparison of 5 commonly used registration centers, whether it is used for interviews or technical selection, is very helpful

明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级

云原生应用的概念和云原生应用的 15 个特征
随机推荐
域名怎么注册备案解析?
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
【32. 图中的层次(图的广度优先遍历)】
单片机开发之静态LED显示
嵌入式环境下并发控制与线程安全
Underwater target detection method based on spatial feature selection
Based on the analysis of the acoustic channel cable tunnel positioning technology
Verilog语法基础HDL Bits训练 08
Digital input and output module DAM-5088
Based on sliding mode control of uncertain neutral system finite time stable
ORA-00600 [13013], [5001], [268] 问题分析及恢复
2022-07-29 顾宇佳 学习笔记 异常处理
久经沙场的程序员居然也被某鱼的假程序员骗了,程序员之间的信任应该是最高的,他一个人毁了这种信任感
概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率
基于空间特征选择的水下目标检测方法
MySQL database maintenance
decodeURIComponent(), eval(), encodeURIComponent()
文本的对齐方式、行高、空间 等总结
听到'演员工作比工人辛苦,吃得也不如工人好?'我笑了
Summary of text alignment, line height, space, etc.