[开发笔记]-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/>");
复制代码

 

转载请注明出处。

 

 

posted @   酷小孩  阅读(3695)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示