469,270 Members | 1,211 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

parse multiple date formats at once

Hi,

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 );

Thanks!
Nov 15 '05 #1
4 6941
REPLY :
instead of datatime use string to accomodate all your
formats then use the CONVERT function Convert.DateTime
(string);

-----Original Message-----
Hi,

Is there a way to parse datetime in string with following possible formatsinto 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 );

Thanks!
.

Nov 15 '05 #2
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
Nov 15 '05 #3
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

Nov 15 '05 #4
Zeng <zz*@nonospam.com> wrote:
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" };


Well, you could start by replacing all slashes, backslahes and dots
with dashes, and then just use one of the "columns" in the above.
Alternatively, split on the various delimiters, see how many values
you've got, parse them with Int32.Parse and then call the DateTime
constructor that takes three int parameters.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Gerrit Holl | last post: by
4 posts views Thread by Richard Hollenbeck | last post: by
7 posts views Thread by memememe | last post: by
3 posts views Thread by Leo Muller | last post: by
4 posts views Thread by Harold Crump | last post: by
29 posts views Thread by gs | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.