Parse date-only value without leading zero in month using "d" format.
using System;
using System.Globalization;
public class Test
{
public static void Main()
{
string dateString, format;
DateTimeOffset result;
CultureInfo provider = CultureInfo.InvariantCulture;
format = "d";
// throw a FormatException because standard short date pattern of invariant culture requires two-digit month.
dateString = "6/15/2010";
try
{
result = DateTimeOffset.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);
}
}
}
Related examples in the same category