Converts string to DateTimeOffset using the specified formats, culture-specific format information, and style.
using System;
using System.Globalization;
public class Test
{
public static void Main()
{
string[] formats = new string[] {@"M/dd/yyyy HH:m zzz", @"MM/dd/yyyy HH:m zzz",
@"M/d/yyyy HH:m zzz", @"MM/d/yyyy HH:m zzz",
@"M/dd/yy HH:m zzz", @"MM/dd/yy HH:m zzz",
@"M/d/yy HH:m zzz", @"MM/d/yy HH:m zzz",
@"M/dd/yyyy H:m zzz", @"MM/dd/yyyy H:m zzz",
@"M/d/yyyy H:m zzz", @"MM/d/yyyy H:m zzz",
@"M/dd/yy H:m zzz", @"MM/dd/yy H:m zzz",
@"M/d/yy H:m zzz", @"MM/d/yy H:m zzz",
@"M/dd/yyyy HH:mm zzz", @"MM/dd/yyyy HH:mm zzz",
@"M/d/yyyy HH:mm zzz", @"MM/d/yyyy HH:mm zzz",
@"M/dd/yy HH:mm zzz", @"MM/dd/yy HH:mm zzz",
@"M/d/yy HH:mm zzz", @"MM/d/yy HH:mm zzz",
@"M/dd/yyyy H:mm zzz", @"MM/dd/yyyy H:mm zzz",
@"M/d/yyyy H:mm zzz", @"MM/d/yyyy H:mm zzz",
@"M/dd/yy H:mm zzz", @"MM/dd/yy H:mm zzz",
@"M/d/yy H:mm zzz", @"MM/d/yy H:mm zzz"};
IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
DateTimeOffset result = new DateTimeOffset();
string input = "12/12/1212";
try
{
result = DateTimeOffset.ParseExact(input, formats, provider, DateTimeStyles.AllowWhiteSpaces);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse {0}.", input);
}
}
}
Related examples in the same category