A DateTime
value doesn't have a format - it's just a value. (Just like an int
isn't inherently decimal or hex - you choose how to format it when you convert it to a string, and the default is to use decimal.)
The string you're passing into DateTime.ParseExact
is the expected input format - and your strings don't have the format "yyyy mm dd". (Note that in date/time format strings, "mm" means minutes, so you'd want "MM" instead of "mm" anyway... but that won't help here.)
Your date format is nearly "d MMM yyyy" (day, short month name, year) using English month names - but the problem is the ordinal part ("st", "nd", "th"). As far as I know there's no simple way of handling that with DateTime.ParseExact
. Instead, I'd probably use a regular expression or simple string replacement to remove the ordinal part, so that you do have a string in the format "d MMM yyyy" and then parse that.
For the string replacement part, the answers to this question are appropriate. So here's a complete example using your sample data:
using System;
using System.Globalization;
class Test
{
static void Main()
{
Console.WriteLine(ParseWithOrdinals("10th Mar 2015"));
Console.WriteLine(ParseWithOrdinals("1st Oct 2018"));
}
private static DateTime ParseWithOrdinals(string input) =>
DateTime.ParseExact(
RemoveOrdinals(input), // Text to parse
"d MMM yyyy", // Format of text
CultureInfo.InvariantCulture); // Expect English month names, Gregorian calendar
// From https://stackoverflow.com/questions/17710561
private static string RemoveOrdinals(string input) =>
input
.Replace("0th", "0")
.Replace("1st", "1")
.Replace("2nd", "2")
.Replace("3rd", "3")
.Replace("11th", "11") // Need to handle these separately...
.Replace("12th", "12")
.Replace("13th", "13")
.Replace("4th", "4")
.Replace("5th", "5")
.Replace("6th", "6")
.Replace("7th", "7")
.Replace("8th", "8")
.Replace("9th", "9");
}
(Note that I haven't formatted the result as yyyy-MM-dd in the output, so you'll get your local date/time format.)