[開發筆記]-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/>");

 

轉載請注明出處。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM