Datetime.Parse

P

Peter Kirk

Hi there

I would like some help with parsing date strings to DateTime structures.

I can see that DateTime has Parse and ParseExact methods - but I am not sure
what is best for me to use, and what parameters I need to supply.

I reveive a string from a web-form, which would look something like this:
"31-12-1999 14:56:22"; and I need to parse it to a DateTime

The ParseExact method has one form that looks like this:
ParseExact(string s, string format, IFormatProvider provider)
but I don't understand what the format-provider is or why it is required,
and how it is different from the supplied format.

Any one have some good suggestions for converting strings to DateTimes?

Thanks for any help,
Peter
 
C

Christiaan van Bergen

Hi Peter,

Normally I will write an answer myself, but this time the msdn-guys have
done a great job :)

Look for info on that ParseExact method at
http://msdn.microsoft.com/library/d.../frlrfsystemdatetimeclassparseexacttopic3.asp
and for more info on datetimeformats at
http://msdn.microsoft.com/library/d...globalizationdatetimeformatinfoclasstopic.asp

Combining the two helptexts should help you along...(look extra careful at
the 'expectedFormats' in the given example of the ParseExact method)

Cheers,
Christiaan
 
S

Scott Coonce

Another reason (I think) for the IFormatProvider: imagine a date like this
"08-07-1999 14:56:22". Is this refering to the seventh day of August (USA
format), or the eighth day July (European).

I once saw an old post enlightening me that there were six (?) ways to
interpret "04-05-03", hence the need for this kind of formating info.

scott
 

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

Top