/// <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