It seems to work but the format list keep growing (see below), I really need
a simpler way to default the date to the first day of the month, and tell
the parser/convertor to aim for month and year if the input string has only
2 numbers. Is there a way to do that?
private static string[] s_Formats = new string[] {
@"MM/dd/yyyy", @"MM-dd-yyyy", @"MM.dd.yyyy", @"MM\dd\yyyy",
@"M/dd/yyyy", @"M-dd-yyyy", @"M.d.yyyy", @"M\d\yyyy",
@"MM/dd/yy", @"MM-dd-yy", @"MM.dd.yy", @"MM\dd\yy",
@"M/dd/yy", @"M-dd-yy", @"M.dd.yy", @"M\dd\yy",
@"MM/yyyy", @"MM-yyyy", @"MM.yyyy", @"MM\yyyy",
@"MM/yy", @"MM-yy", @"MM.yy", @"MM\yy",
@"M/yy", @"M-yy", @"M.YY", @"M\YY" };
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Zeng <zz*@nonospam.com> wrote: Is there a way to parse datetime in string with following possible
formats into a DateTime object?
- MM/DD/YYYY
- MM/DD/YY
- MM/YY
- MM/YYYY
This line of code would throw if I pass "08/03"
DateTime result = DateTime.Parse( s );
Use DateTime.ParseExact, which takes an array of formats to try one by
one.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too