首页 » 技术分享 » 美国夏令时转UTC时间

美国夏令时转UTC时间

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //美国时间
            string date = "05:17:46 Jun 20, 2016 PDT";
            string[] dts = date.Split(',');
            string year = dts[1].Substring(0, 5);
            //两种方法截取时间
            string newdate = dts[0] + year;
            //string  newdate = date.Remove(21);
            DateTime dt = Convert.ToDateTime(newdate);
            DateTime dt2 = PSTConvertToGMT(dt);
            if (date.Contains("PDT"))
            {
                //PDT 比PST早一个小时
                dt2 = dt2.AddHours(-1);
            }
            string str = dt2.ToString("yyyy-MM-dd hh:mm:ss");
            Console.WriteLine(str);
        }

        /// <summary>
        /// 转化PST时间为GMT(也就是UTC时间)
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        private static DateTime PSTConvertToGMT(DateTime dateTime)
        {
            TimeZoneInfo timeZoneSource = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
            TimeZoneInfo timeZoneDestination = TimeZoneInfo.FindSystemTimeZoneById("UTC");
            return TimeZoneInfo.ConvertTime(dateTime, timeZoneSource, timeZoneDestination);
        }
        /// <summary>
        /// 转化UTC时间为PST时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        private static DateTime ConvertToPST(DateTime dateTime)
        {
            return System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, "Pacific Standard Time");
        }

        /// <summary>
        /// 转化PST时间为UTC时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        private static DateTime PSTConvertToUTC(DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeToUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
        }


    }
}
  

转载自原文链接, 如需删除请联系管理员。

原文链接:美国夏令时转UTC时间,转载请注明来源!

0