0

I am looking to convert this time: Mon, 25 03 2013 00:00:00 GMT into US EST. I know that this has been posted before here:Time zone conversion in C# but I am still getting errors having to do something with the string not being exactly right.

Here is my code:

var dateString = "Mon, 25 03 2013 00:00:00 GMT";
var date = Convert.ToDateTime(dateString);
var result = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time"));
Console.WriteLine(result);

Here is my error:

Unhandled Exception: System.FormatException: String was not recognized as a valid DateTime.
at System.DateTime.Parse (System.String s, IFormatProvider provider, DateTimeStyles styles) [0x00000] in :0 
at System.DateTime.Parse (System.String s, IFormatProvider provider) [0x00000] in :0 
at System.DateTime.Parse (System.String s) [0x00000] in :0 

at System.Convert.ToDateTime (System.String value) [0x00000] in :0 at Program.Main () [0x00000] in :0

I have tried changing the date from 03 to Mar but I was unable to even get that to work. I have also tried the code in the other forum just to see if that even worked, but I was unsuccessful. I fully understand what the code is trying to do, but I just don't understand why there are errors being thrown.

Any help would be much appreciated! Thanks!

Community
  • 1
  • 1
scapegoat17
  • 5,509
  • 14
  • 55
  • 90
  • 2
    Much of what you described here is just repeating what you already asked in your other question. I don't see any question here about time zone conversion, so I'm voting to close. – Matt Johnson-Pint Jul 09 '13 at 21:04
  • Your code is working on my machine. I just copied and try to run it successfully executed :) – Sachin Jul 09 '13 at 21:05
  • Have you tried using this overload of DateTime.Parse:http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx? – auujay Jul 09 '13 at 21:07
  • Also, the time zone id you are looking for is just `"Eastern Standard Time"`. Drop the `"US "` from the front of it. – Matt Johnson-Pint Jul 09 '13 at 21:08
  • @MattJohnson: If at first SO doesn't solve your problem for you, just ask ask again. :\ – Michael Bray Jul 09 '13 at 21:13
  • @MattJohnson I felt that it was a little different than what was being previously asked before. Sorry for the repeat though. – scapegoat17 Jul 09 '13 at 21:14
  • Did you try my answer from your other question? If it doesn't work, please comment there. When I suggested you ask separately for the timezone issues, I didn't mean for you to repeat the whole parsing problem again. – Matt Johnson-Pint Jul 09 '13 at 21:15
  • @MattJohnson I did not realize that is what you meant. I will hop back to the other forum. My apologies... I do really appreciate the help though. – scapegoat17 Jul 09 '13 at 21:22
  • 1
    No problem. We're here to help! – Matt Johnson-Pint Jul 09 '13 at 21:24

3 Answers3

1

Try parsing your date string to something like this:

 DateTime date = DateTime.ParseExact(datestring, "ddd dd MMM yyyy h:mm tt zzz", CultureInfo.InvariantCulture);
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
0
var dateString = "Mon, 25 03 2013 00:00:00 GMT";

should be this:

var dateString = "Mon, 25 Mar 2013 00:00:00 GMT";

EDIT: Fixed month.

Th3BFG
  • 305
  • 1
  • 12
0

If the string will ALWAYS end in GMT then you can use ParseExact:

var dateString = "Mon, 25 03 2013 00:00:00 GMT";
var date = DateTime.ParseExact(dateString,
                               "ddd, dd MM yyyy hh:mm:ss GMT", 
                               CultureInfo.InvariantCulture);
var result = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time"));
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I do not really understand what you are doing with `CultureInfo.InvariantCulture` but it went away when i changed that to `null`. Even still both throw errors for me. – scapegoat17 Jul 09 '13 at 21:16
  • `CultureInfo.InvariantCulture` is used to specify an unchanging (not customizable) culture that is not specific to any one region. It uses English month and day names. Did you change `Convert.ToDateTime` to `DateTime.ParseExact`? – D Stanley Jul 09 '13 at 21:27
  • okay, I understand and yes I did change it – scapegoat17 Jul 09 '13 at 21:28
  • So what error are you getting now? – D Stanley Jul 09 '13 at 21:48