Alex Gordon
Alex Gordon

Reputation: 60882

converting string to datetime issue c#

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?

Upvotes: 0

Views: 196

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503769

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?

Upvotes: 1

Ed Bayiates
Ed Bayiates

Reputation: 11230

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

Upvotes: 0

Related Questions