[开发笔记]-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 @ 2013-06-13 18:40  酷小孩  阅读(3686)  评论(0编辑  收藏  举报