Wednesday, June 27, 2007

Little Questions for the Bored: DateTime.Parse

This will be the first post in the series "Little Questions for the Bored". I want to see the answers to the questions in the comments. Obviously, if you're going to be a moron and copy and paste the answer straight from Google, you'll be banned and signed up for a 1-way ticket to hell, care of me. Heh.

On to the question:

DateTime dt = DateTime.Parse("01/02/2007");

What is the value of dt.Day?
What is the value of dt.Month?

Why is it not good to use the above DateTime.Parse() method like it is shown?

Labels:

2 Comments:

Blogger Derik said...

Day is 2, month is 1.

Bad because you are making the assumption on the format of the date. In this case MM/DD/YYYY. What is you needed/wanted DD/MM/YYYY

June 28, 2007 9:53 AM

 
Blogger Jonathan Bates said...

What it shows is going to depend upon the culture that the framework is running on in order to determine the date format. It is overloaded to provide the culture format so that you can get the date for the culture you want (so your app in the states could display dates in europeon fashion).

I dont think there is anything inherently wrong with the function, but without including a format provider you run the risk of (13/12/2007) not being parsed as December 13th, 2007 (euro style) and it throwing an exception.

June 30, 2007 9:55 AM

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home