Conversion of local time and GMT (UTC) time

Source: Internet
Author: User
Tags datetime local time
        <summary>
        ///local time to GMT time
        ///</summary> public
        static string togmtstring (DateTime dt)
        {return
            dt. ToUniversalTime (). ToString ("R");
        }

string s = togmtstring (DateTime.Now);

local time: 2011-9-29 15:04:39

The converted time is: Thu, Sep 07:04:39 GMT

        <summary>
        ///local time to GMT format
        ///</summary> public
        static string Togmtformat (DateTime DT )
        {return
            dt. ToString ("R") + dt. ToString ("zzz"). Replace (":", "");
        }

string s = Togmtformat (DateTime.Now);

local time: 2011-9-29 15:04:39

The converted time is: Thu, Sep 15:04:39 gmt+0800


        <summary>///GMT time to local time///</summary>///<param name= "GMT" > String form
        GMT time </param>///<returns></returns> public static DateTime gmt2local (string GMT)
            {DateTime dt = Datetime.minvalue;
                try {string pattern = ""; if (GMT). IndexOf ("+0")!=-1) {GMT = GMT.
                    Replace ("GMT", "");
                Pattern = "DDD, dd MMM yyyy HH ': ' mm ': ' ss zzz"; } if (GMT. ToUpper ().
                IndexOf ("GMT")!=-1 {pattern = "ddd, dd MMM yyyy HH ': ' mm ': ' SS ' GMT '"; } if (Pattern!= "") {dt = DateTime.ParseExact (GMT, pattern, Syst Em.
                    Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal); DT = dt.
                ToLocalTime (); }
                else {dt = Convert.todatetime (GMT);
        } catch {} return dt; }

DateTime dt1 = gmt2local ("Thu, Sep 07:04:39 GMT");

Converted Dt1:2011-9-29 15:04:39

DateTime DT2 = gmt2local ("Thu, Sep 15:04:39 gmt+0800");

Converted DT2:2011-9-29 15:04:39


Author: Zhu Huazhen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.