parse multiple date formats at once

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zeng

    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!


  • dabuskol

    #2
    parse multiple date formats at once

    REPLY :
    instead of datatime use string to accomodate all your
    formats then use the CONVERT function Convert.DateTim e
    (string);

    [color=blue]
    >-----Original Message-----
    >Hi,
    >
    >Is there a way to parse datetime in string with[/color]
    following possible formats[color=blue]
    >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!
    >
    >
    >.
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: parse multiple date formats at once

      Zeng <zzy@nonospam.c om> wrote:[color=blue]
      > 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 );[/color]

      Use DateTime.ParseE xact, which takes an array of formats to try one by
      one.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Zeng

        #4
        Re: parse multiple date formats at once

        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]" <skeet@pobox.co m> wrote in message
        news:MPG.1a1aaa 0ade942ca3989a6 f@msnews.micros oft.com...[color=blue]
        > Zeng <zzy@nonospam.c om> wrote:[color=green]
        > > Is there a way to parse datetime in string with following possible[/color][/color]
        formats[color=blue][color=green]
        > > 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 );[/color]
        >
        > Use DateTime.ParseE xact, which takes an array of formats to try one by
        > one.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: parse multiple date formats at once

          Zeng <zzy@nonospam.c om> wrote:[color=blue]
          > 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" };[/color]

          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 - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...