Conversion between local time and GMT (UTC) Time

Source: Internet
Author: User
/// <Summary> /// convert the local time to GMT // </Summary> Public static string togmtstring (datetime DT) {return DT. touniversaltime (). tostring ("R ");}

String S = togmtstring (datetime. Now );

// Local time: 15:04:39

// The converted time is Thu, 29 Sep 2011 07:04:39 GMT.

/// <Summary> /// time when the local time is converted to the GMT format /// </Summary> Public static string togmtformat (datetime DT) {return DT. tostring ("R") + dt. tostring ("Zzz "). replace (":","");}

String S = togmtformat (datetime. Now );

// Local time: 15:04:39

// The converted time is Thu, 29 Sep 2011 15:04:39 GMT + 0800

/// <Summary> /// convert the GMT time to the local time /// </Summary> /// <Param name = "GMT"> string-type 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, system. globalization. cultureinfo. invariantculture, system. globalization. datetimestyles. adjusttouniversal); dt = DT. tolocaltime ();} else {dt = convert. todatetime (GMT) ;}} catch {} return DT ;}

Datetime dt1 = gmt2local ("Thu, 29 Sep 2011 07:04:39 GMT ");

// The converted dt1 is: 15:04:39

Datetime dt2 = gmt2local ("Thu, 29 Sep 2011 15:04:39 GMT + 0800 ");

// The converted dt2 is: 15:04:39

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.