0

I have a string

Tue May 13 2014 00:00:00 GMT+0700 (SE Asia Standard Time)

I want to convert to DateTime in C#. I use:

Convert.ToDateTime and DateTime.ParseExact("Tue May 13 2014 00:00:00 GMT+0700 (SE Asia Standard Time)","ddd MMM dd yyyy HH:mm:ss GMT+zzzz",System.Globalization.CultureInfo.InvariantCulture)

but it is not work :(

Please help me solve this issue.

Thanks so much.

An Hv
  • 897
  • 1
  • 6
  • 7
  • Thanks Karim I try it. but it not working with me :( – An Hv May 29 '14 at 04:54
  • What is a "String dateTime of javascript"? Isn't that just a string? The format of the string created by javascript's [*Date.toString*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.5.2) is specified to be implementation dependent, so don't expect it to be consistent across implementations (it isn't). E.g. I get "Thu May 29 14:59:32 UTC+1000 2014". – RobG May 29 '14 at 04:58

4 Answers4

0

Stripe off the end of the string and use this :

var Date = "Tue May 13 2014 00:00:00 GMT+0700";
var FormattedDate = DateTime.ParseExact(Date,"ddd MMM dd yyyy HH:mm:ss 
'GMT'zzz",System.Globalization.CultureInfo.InvariantCulture);

Hope this helps you. Cheers.

Gaurav Deochakke
  • 2,265
  • 2
  • 21
  • 26
0

You must remove the TimeZone ID to be able to use a standard C# formatter.

This example works fine (your date, without the TimeZone ID):

var myDateString = "Tue May 13 2014 00:00:00 GMT+0700";

Console.WriteLine(DateTime.ParseExact(myDateString,"ddd MMM d yyyy HH:mm:ss GMTzzzzz",CultureInfo.InvariantCulture));
quantdev
  • 23,517
  • 5
  • 55
  • 88
0

The problem seems to be string literal GMT and (SE Asia Standard Time). You have parse these literals as well You can use single quotes to treat as string literal in the DateTime string format.

For example you have (SE Asia Standard Time) in the date time string then you can wrap it in single quotes in custom DateTime format for GMT like '(SE Asia Standard Time)'.

DateTime dt = DateTime.ParseExact("Tue May 13 2014 00:00:00 GMT+0700 (SE Asia Standard Time)",
                    "ddd MMM dd yyyy HH:mm:ss 'GMT'zzzz '(SE Asia Standard Time)'",
                    System.Globalization.CultureInfo.InvariantCulture);
Adil
  • 146,340
  • 25
  • 209
  • 204
0

This my code:

  var registerDateString = "Tue May 13 2014 00:00:00 GMT+0700 (SE Asia Standard Time)";
  var timeZoneIndex = registerDateString.IndexOf("(", System.StringComparison.Ordinal);
  var timeZoneId = registerDateString.Substring(timeZoneIndex);
  var dateTimeString = registerDateString.Substring(0, timeZoneIndex - 1);
  var FormattedDate = DateTime.ParseExact(dateTimeString,"ddd MMM dd yyyy HH:mm:ss 'GMT'zzz",System.Globalization.CultureInfo.InvariantCulture);

Thanks for your help everyone.

An Hv
  • 897
  • 1
  • 6
  • 7
  • You can directly parse the datetime string you have including (SE Asia Standard Time) – Adil May 29 '14 at 06:10