当前位置:网站首页>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);
}
}边栏推荐
- TensorFlow自定义训练函数
- 11 年膨胀 575 倍,微信为何从“小而美”变成了“大而肥”?
- C language - bitwise operations
- 不用if分支对同一个变量做判断的方法
- 周鸿祎:微软抄袭了360安全模式 所以成为美国最大的安全公司
- API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
- decodeURIComponent()、eval()、encodeURIComponent()
- Jingdong school recruited written test questions + summary of knowledge points
- 文本的对齐方式、行高、空间 等总结
- 流水线上的农民:我在工厂种蔬菜
猜你喜欢

Apifox 生成接口文档 教程与操作步骤

文本的对齐方式、行高、空间 等总结

The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust

Difference between C# enumeration type and xaml

云原生应用的概念和云原生应用的 15 个特征

概率论的学习整理5:贝叶斯(bayes)法则和贝叶斯概率

EA中的业务对象和业务实体你分得清吗?

Verilog grammar basics HDL Bits training 07

STM32F1读取MLX90632非接触式红外温度传感器

Leetcode 125. 验证回文串
随机推荐
Scheduling of combined electric-heating system based on multi-objective two-stage stochastic programming method
Niuke-TOP101-BM42
The method of judging the same variable without the if branch
Matlab基础(3)——元胞与结构体
自定义查询--关于倒排索引的研究
C#调用explorer.exe打开指定目录
idea的package没有空心
基于空间特征选择的水下目标检测方法
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
【ASP.NET Core】选项类的依赖注入
Detailed explanation of @RequestBody and @ResponseBody
我又造了个轮子:GrpcGateway
基于滑模控制的不确定中立型系统有限时间稳定
Meituan internal push + school recruitment written test + summary of knowledge points
Based on sliding mode control of uncertain neutral system finite time stable
Hu-cang integrated e-commerce project (1): project background and structure introduction
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
Vim plugin GrepIt
Vim plugin GrepIt
又爆神作!阿里爆款MySQL高级宝典开源,直抵P7