Safe conversion from string to DateTime

  • Thread starter Thread starter Hans Merkl
  • Start date Start date
H

Hans Merkl

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.ToDateTime 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.
 
use DateTime.Parse or DateTime.ParseExact

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 DateTimeFormatInfo object.

For DateTime.ParseExact, 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.Globalization.CultureInfo("fr-FR", true);
string[] expectedFormats = {"g", "G", "f", "F"};

// This is DD/MM/YYYY format
NewDate = DateTime.ParseExact("27/10/1961 08:47:00",
expectedFormats, format,
System.Globalization.DateTimeStyles.AllowWhiteSpaces);
Console.WriteLine("Parsed DD/MM/YY: {0}", NewDate.ToString());


Example for DateTime.ParseExact

try
{
NewDate = DateTime.ParseExact(
"10/27/1961",
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpaces);
Console.WriteLine(NewDate.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Hope this helps.

SCDeveloper
http://www.sharingcorner.com
 
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.ParseExact()

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,
 
I have tried DateTime.Parse but it threw an exception on "12 noon".

Hans
 
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top