2

I have following string ,

Thu Sep 24 2015 00:00:00 GMT+0530 (IST)

I tried with following but it's faling.

   var twDate = DateTime.Parse("Thu Sep 24 2015 00:00:00 GMT+0530 (IST) ");

Can not use replace , as IST wont be fixed. Any Ideas?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Simsons
  • 12,295
  • 42
  • 153
  • 269
  • 1
    Is the time zone part always in brackets after a space though? If so, you could detect *that* and parse the rest. (I'd suggest parsing it to a DateTimeOffset, given that that's the information being presented...) – Jon Skeet Sep 24 '15 at 15:06
  • http://stackoverflow.com/questions/5615538/parse-a-date-string-into-a-certain-timezone-supporting-daylight-saving-time – Ruchi Sep 24 '15 at 15:09
  • http://stackoverflow.com/questions/241789/parse-datetime-with-time-zone-of-form-pst-cest-utc-etc – Ruchi Sep 24 '15 at 15:09

2 Answers2

2

You need to trim the time zone abbreviation off using normal string operations, then specify a custom date and time format string. For example:

// After trimming
string text = "Thu Sep 24 2015 00:00:00 GMT+0530";
var dto = DateTimeOffset.ParseExact(
    text,
    "ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
    CultureInfo.InvariantCulture);
Console.WriteLine(dto);

Note the use of CultureInfo.InvariantCulture here - you almost certainly don't want to parse using the current thread's current culture.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

If your string always has the same format and length, you could use this to get UTC:

String dtstr = "Thu Sep 24 2015 00:00:00 GMT+0530 (IST)";
int zoneMin = int.Parse(dtstr.Substring(29, 2)) * 60 + int.Parse(dtstr.Substring(31, 2));
if (dtstr.Substring(28, 1) == "+") zoneMin = -zoneMin;
DateTime dt = DateTime.Parse(dtstr.Substring(0, 24)).AddMinutes(zoneMin);
navigator
  • 1,678
  • 16
  • 29