Safe conversion from string to DateTime

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

    Safe conversion from string to DateTime

    Does anybody know of a library that can handle strings pf various formats
    and conver them to a DateTime value? The strings are coming from a webform
    and I can't restrict the input (it's not my form).

    I have been using Convert.ToDateT ime but it choked on "12:00 noon".

    I am looking for a function that can make sense out of anything that looks
    like a date/time. Outlook is pretty good at it when you enter an
    appointment with the GUI, but it doesn't expose this functionality via API.
  • SCDeveloper

    #2
    Re: Safe conversion from string to DateTime

    use DateTime.Parse or DateTime.ParseE xact

    Parse ignores leading and trailing white space, and unrecognized
    characters if possible, and it fills in missing information with the
    corresponding current date and time values. Parse will throw a
    FormatException , though, if it's unable to decipher the string you send
    to it.

    DateTime.Parse will parse a valid date and time from a string. The
    string must contain the representation of a date and time in one of the
    formats described in the DateTimeFormatI nfo object.

    For DateTime.ParseE xact, the string that you pass to it must exactly
    match the format that you specify in the IFormatProvider parameter.

    Example for DateTime.Parse

    NewDate = DateTime.Parse( "10/27/61 08:47");
    NewDate = DateTime.Parse( "10/1961");
    NewDate = DateTime.Parse( "27 October 1961 8:47 pm");

    IFormatProvider format = new
    System.Globaliz ation.CultureIn fo("fr-FR", true);
    string[] expectedFormats = {"g", "G", "f", "F"};

    // This is DD/MM/YYYY format
    NewDate = DateTime.ParseE xact("27/10/1961 08:47:00",
    expectedFormats , format,
    System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
    Console.WriteLi ne("Parsed DD/MM/YY: {0}", NewDate.ToStrin g());


    Example for DateTime.ParseE xact

    try
    {
    NewDate = DateTime.ParseE xact(
    "10/27/1961",
    expectedFormats ,
    format,
    System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
    Console.WriteLi ne(NewDate.ToSt ring());
    }
    catch (Exception e)
    {
    Console.WriteLi ne(e.Message);
    }

    Hope this helps.

    SCDeveloper


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Safe conversion from string to DateTime


      Hi,

      What you want cannot be done, how do you interprete 1/1/11 ?
      You need to know the format the date is represented in, then you can use
      the correct Format string in DateTime.ParseE xact()

      Can you talk with the webform developer regarding the structure? you could
      even provide him with the correct format string and then you can get as
      input both the string to be converted as well as the srting representing the
      date structure


      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation



      "Hans Merkl" <hans_merkl@yah oo.com> wrote in message
      news:1161jo434m jp$.hs5i30h17qo a$.dlg@40tude.n et...[color=blue]
      > Does anybody know of a library that can handle strings pf various formats
      > and conver them to a DateTime value? The strings are coming from a webform
      > and I can't restrict the input (it's not my form).
      >
      > I have been using Convert.ToDateT ime but it choked on "12:00 noon".
      >
      > I am looking for a function that can make sense out of anything that looks
      > like a date/time. Outlook is pretty good at it when you enter an
      > appointment with the GUI, but it doesn't expose this functionality via
      > API.[/color]


      Comment

      • Hans Merkl

        #4
        Re: Safe conversion from string to DateTime

        I have tried DateTime.Parse but it threw an exception on "12 noon".

        Hans


        On 5 May 2005 07:15:22 -0700, SCDeveloper wrote:
        [color=blue]
        > use DateTime.Parse or DateTime.ParseE xact
        >
        > Parse ignores leading and trailing white space, and unrecognized
        > characters if possible, and it fills in missing information with the
        > corresponding current date and time values. Parse will throw a
        > FormatException , though, if it's unable to decipher the string you send
        > to it.
        >
        > DateTime.Parse will parse a valid date and time from a string. The
        > string must contain the representation of a date and time in one of the
        > formats described in the DateTimeFormatI nfo object.
        >
        > For DateTime.ParseE xact, the string that you pass to it must exactly
        > match the format that you specify in the IFormatProvider parameter.
        >
        > Example for DateTime.Parse
        >
        > NewDate = DateTime.Parse( "10/27/61 08:47");
        > NewDate = DateTime.Parse( "10/1961");
        > NewDate = DateTime.Parse( "27 October 1961 8:47 pm");
        >
        > IFormatProvider format = new
        > System.Globaliz ation.CultureIn fo("fr-FR", true);
        > string[] expectedFormats = {"g", "G", "f", "F"};
        >
        > // This is DD/MM/YYYY format
        > NewDate = DateTime.ParseE xact("27/10/1961 08:47:00",
        > expectedFormats , format,
        > System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
        > Console.WriteLi ne("Parsed DD/MM/YY: {0}", NewDate.ToStrin g());
        >
        >
        > Example for DateTime.ParseE xact
        >
        > try
        > {
        > NewDate = DateTime.ParseE xact(
        > "10/27/1961",
        > expectedFormats ,
        > format,
        > System.Globaliz ation.DateTimeS tyles.AllowWhit eSpaces);
        > Console.WriteLi ne(NewDate.ToSt ring());
        > }
        > catch (Exception e)
        > {
        > Console.WriteLi ne(e.Message);
        > }
        >
        > Hope this helps.
        >
        > SCDeveloper
        > http://www.sharingcorner.com[/color]

        Comment

        • Hans Merkl

          #5
          Re: Safe conversion from string to DateTime

          1/1/11 is a tough one :-). I'd be happy to have something that matches
          Outlook's functionality. It can handle "noon", "midnight" and I don't know
          what else. I now can handle these with my code but people might come up
          with other variations.

          Unfortunately the mails are coming from a company that hasn't been
          responsive to change requests in the past so I don't think I can change the
          formats of the mails. It's one of my selling points that I can deal with
          their mails as is...

          On Thu, 5 May 2005 10:47:13 -0400, Ignacio Machin ( .NET/ C# MVP ) wrote:
          [color=blue]
          > Hi,
          >
          > What you want cannot be done, how do you interprete 1/1/11 ?
          > You need to know the format the date is represented in, then you can use
          > the correct Format string in DateTime.ParseE xact()
          >
          > Can you talk with the webform developer regarding the structure? you could
          > even provide him with the correct format string and then you can get as
          > input both the string to be converted as well as the srting representing the
          > date structure
          >
          >
          > cheers,[/color]

          Comment

          Working...