0

i have a string like this:

1/1/2011

i need to convert it to DateTime

i have so far tried with no luck:

DateTime.ParseExact("1/1/2011"
, "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture)

what am i doing wrong?

1
  • 2
    What error are you getting, and can you post some code so we can see exactly what you're doing? Commented Jul 13, 2011 at 21:28

2 Answers 2

1

That code should work absolutely fine, and does for me:

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        DateTime dt = DateTime.ParseExact("1/1/2011", 
                                          "M/d/yyyy",
                                          CultureInfo.InvariantCulture);
        Console.WriteLine(dt);
    }
}

You say you've tried "with no luck" - what happens for you? Can you come up with a similar short but complete program that fails?

Sign up to request clarification or add additional context in comments.

Comments

0

Use DateTime.Parse (or better, DateTime.TryParse) instead of ParseExact.

2 Comments

Did you use the (string, out DateTime) form? That should work. Your format specifier is part of the problem.
The format specifier is fine for the data given. Using ParseExact is the right thing to do when you know the format.

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.