简体   繁体   中英

How to parse string which contains GMT to DateTime?

How can I convert this string:

string aa ="Thu Jul 02 2015 00:00:00 GMT+0100 (GMT Standard Time)"; 

into a DateTime.

I tried to use the Convert.ToDateTime(aa); but didn't work Thanks.

EDIT: error message - The string was not recognized as a valid DateTime

Using DateTime.Parse Method:

    using System;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = {"2008-05-01T07:34:42-5:00", 
                              "2008-05-01 7:34:42Z", 
                              "Thu, 01 May 2008 07:34:42 GMT"};
      foreach (string dateString in dateStrings)
      {
         DateTime convertedDate = DateTime.Parse(dateString);
         Console.WriteLine("Converted {0} to {1} time {2}", 
                           dateString, 
                           convertedDate.Kind.ToString(), 
                           convertedDate);
      }                              
   }
}
// These calls to the DateTime.Parse method display the following output: 
//  Converted 2008-05-01T07:34:42-5:00 to Local time 5/1/2008 5:34:42 AM 
//  Converted 2008-05-01 7:34:42Z to Local time 5/1/2008 12:34:42 AM 
//  Converted Thu, 01 May 2008 07:34:42 GMT to Local time 5/1/2008 12:34:42 AM  

You can use DateTime.TryParseExact with the correct format string:

string dtString = "Thu Jul 02 2015 00:00:00 GMT+0100";
string format = "ddd MMM dd yyyy HH:mm:ss 'GMT'K";
DateTime date;
bool validFormat = DateTime.TryParseExact(dtString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date);
Console.Write(validFormat ? date.ToString() : "Not a valid format");

If the string contains (GMT Standard Time) at the end you could simply remove it first:

dtString = dtString.Replace("(GMT Standard Time)", "").Trim();

or use this format pattern:

string format = "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'";

Further informations: https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Since you have an UTC offset in your string, I would prefer to parse DateTimeOffset instead of DateTime . And there is no way to parse your GMT and (GMT Standard Time) parts without escape them.

Both DateTime and DateTimeOffset are timezone awareness by the way. DateTimeOffset little bit better than DateTime for this situation. It has UTC Offset but this doesn't guaranteed the timezone information because different timezones can have same offset value.

Even if they are, time zone abbreviations are not standardized. CST has several meanings for example.

string s = "Thu Jul 02 2015 00:00:00 GMT+01:00 (GMT Standard Time)";
DateTimeOffset dto;
if (DateTimeOffset.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(GMT Standard Time)'", 
                                 CultureInfo.InvariantCulture,
                                 DateTimeStyles.None, out dto))
{
     Console.WriteLine(dto);
}

Now, you have a DateTimeOffset as {02.07.2015 00:00:00 +01:00}

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM