Reputation: 1690
How do I parse this date to DateTime in C#?
2014-09-26t1505
I tried the following way:
DateTime pickupDate = DateTime.ParseExact("2014-09-26t1505", "t",
CultureInfo.CreateSpecificCulture("en-us"));
But looks like this is wrong as I am getting following error:
String was not recognized as a valid DateTime.
Upvotes: 0
Views: 307
Reputation: 4514
DateTime pickupDate = ParseExact("2014-09-26t1505", "yyyy-MM-ddtHHmm", CultureInfo.InvariantCulture)
Upvotes: -1
Reputation: 2583
You should do like this.
string dateString = "2014-09-26t1505";
string format = "yyyy-MM-dd't'HHmm";
CultureInfo provider = CultureInfo.InvariantCulture;
try {
DateTime result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
To use ParseExact you have to actually specify the format you are using. Not sure why you just put "t". See MSDN for more info and code examples.
Upvotes: 4