0

i want to convert this string into DateTime.

 Tue Aug 19 15:05:05 +0000 2008

I have tried the following code, but not getting the proper value.

string strDate = "Tue Aug 19 15:05:05 +0000 2008";
DateTime date;
DateTime.Parse(strDate,out date);
1

2 Answers 2

10
DateTime date = DateTime.ParseExact(
    "Tue Aug 19 15:05:05 +0000 2008", 
    "ddd MMM dd HH:mm:ss zzz yyyy", 
    CultureInfo.InvariantCulture
);

For more safety use TryParseExact method:

string str = "Tue Aug 19 15:05:05 +0000 2008";
string format = "ddd MMM dd HH:mm:ss zzz yyyy";
DateTime date;
if (DateTime.TryParseExact(str, format, CultureInfo.InvariantCulture, 
    DateTimeStyles.None, out date))
{
    Console.WriteLine(date.ToString());
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nicely formatted to show the correlation between the actual data and the date time info string.
6

Take a look at DateTime.Parse and DateTime.ParseExact.

3 Comments

@Mark I was already doing that but not getting the proper values.
@Mark thats why i posted question on SO
Yes, I missed that when I first looked at your question, which is why I edited my answer to include ParseExact—but Darin's answer is much more comprehensive anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.