[开发笔记]-unix时间戳、GMT时间与datetime类型时间之前的转换
前段时间项目中涉及到了MySql和MsSql数据类型之间的转换,最近又在研究新浪微博的API,涉及到了带有时区的GMT时间类型的转换,所以,特记录于此,以备日后查询。
一:UNIX时间戳与datetime时间之间的转换
1. 将Unix时间戳转换为DateTime类型时间
方法一:
/// <summary> /// 将Unix时间戳转换为DateTime类型时间 /// </summary> /// http://www.cnblogs.com/babycool /// <param name="d"></param> /// <returns></returns> public static DateTime ConvertIntToDateTime(double d) { System.DateTime time = System.DateTime.MinValue; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); time = startTime.AddSeconds(d); return time; }
方法二:
/// <summary> /// 将Unix时间戳转换为DateTime类型时间 /// </summary> /// http://www.cnblogs.com/babycool /// <param name="time"></param> /// <returns></returns> static DateTime ConvIntToDateTime(long time) { DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳 long t = (time + 8 * 60 * 60) * 10000000 + timeStamp.Ticks; DateTime dt = new DateTime(t); return dt; }
2.在SQL Server Management Studio 中查询并转换:
--将Unix时间戳转换为dateline类型 select top 10 DATEADD(SS,regdate,'1970-01-01 00:00:00') from dbo.uc_members
3. 将DateTime时间格式转换为Unix时间戳格式
/// <summary> /// 将DateTime时间格式转换为Unix时间戳格式 /// </summary> /// http://www.cnblogs.com/babycool /// <param name="time"></param> /// <returns></returns> public static double ConvertDateTimeToInt(System.DateTime time) { double intResult = 0; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); intResult = (time - startTime).TotalSeconds; return intResult; }
二:将新浪微博中带有时区的GMT时间转换为DateTime
/// <summary> /// 将新浪微博中带有时区的GMT时间转换为DateTime /// </summary> /// http://www.cnblogs.com/babycool /// <param name="dateString">微博时间字符串</param> /// <returns>DateTime</returns> public static DateTime ParseUTCDate(string dateString) { System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; DateTime dt = DateTime.ParseExact(dateString, "ddd MMM dd HH:mm:ss zzz yyyy", provider); return dt; }
三:转换效果:
相应代码:
//新浪微博返回的时间是带有时区的GMT时间,转换为datetime类型时间格式: // GMT时间: Tue May 31 17:46:55 +0800 2011 Response.Write("将GMT时间转换为datetime类型时间:"); Response.Write(ParseUTCDate("Tue May 31 17:46:55 +0800 2011").ToString()); Response.Write("<br/>"); //UNIX时间戳 1176686120 Response.Write("将UNIX时间戳转换为datetime类型时间:"); Response.Write(ConvertIntToDateTime(1176686120)); Response.Write("<br/>"); Response.Write("方法二:"); Response.Write(ConvIntToDateTime(1176686120)); Response.Write("<br/>"); Response.Write("将datetime类型时间转换为UNIX时间戳:"); Response.Write(ConvertDateTimeToInt(ConvertIntToDateTime(1176686120))); Response.Write("<br/>");
转载请注明出处。

作者:酷小孩
出处:http://www.cnblogs.com/babycool/
本文首发博客园,版权归作者跟博客园共有。
转载必须保留本段声明,并在页面显著位置给出本文链接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架