Reputation: 60882
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
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
Reputation: 11230
Use DateTime.Parse (or better, DateTime.TryParse) instead of ParseExact.
Upvotes: 0