469,607 Members | 2,068 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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.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.
Nov 17 '05 #1
4 17920
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.AllowWhiteSpac es);
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.AllowWhiteSpac es);
Console.WriteLine(NewDate.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Hope this helps.

SCDeveloper
http://www.sharingcorner.com

Nov 17 '05 #2

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,

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

"Hans Merkl" <ha********@yahoo.com> wrote in message
news:11*****************************@40tude.net...
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.

Nov 17 '05 #3
I have tried DateTime.Parse but it threw an exception on "12 noon".

Hans
On 5 May 2005 07:15:22 -0700, SCDeveloper wrote:
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.AllowWhiteSpac es);
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.AllowWhiteSpac es);
Console.WriteLine(NewDate.ToString());
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

Hope this helps.

SCDeveloper
http://www.sharingcorner.com

Nov 17 '05 #4
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:
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,

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

50 posts views Thread by z. f. | last post: by
18 posts views Thread by David Gacek | last post: by
4 posts views Thread by apoc69 | last post: by
10 posts views Thread by Jeff | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.