Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I am trying to set a date as "01/01/1900" in the initializer of a class.
For this I am using following code:
C#
private DateTime Delivery_Date = DateTime.ParseExact("01/01/1900", "MM/dd/yyyy", null);

This throws error.
My Systems date format is dd-MM-yyyy.
When I change the System date format from dd-MM-yyyy to dd/MM/yyyy. The above code works fine.

Please help.

Thanks,
Nitin
Posted
Updated 11-Jun-13 6:40am
v3
Comments
Richard C Bishop 11-Jun-13 11:06am    
Why are you parsing a datetime to a datetime?

Why not use:
C#
private DateTime Delivery_Date = new DateTime(1900, 1, 1);
 
Share this answer
 
Comments
Thomas Daniels 11-Jun-13 12:51pm    
Good answer, +5!
Matt T Heffron 11-Jun-13 14:29pm    
+5
NMuch 12-Jun-13 0:54am    
It works but still not clear, Why it is not parsing the date properly if I am providing the format.
OriginalGriff 12-Jun-13 4:10am    
Simple, all you had to do was read the documentation:
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
Which says:
"If provider is null, the CultureInfo object that corresponds to the current culture is used."

Since you don't specify InvariantCulture, your local setting takes priority.
Though it doesn't throw an error on my system, and my date format is dd/MM/yyyy, so that part may be due to your .NET environment being different in some way from mine.
It does seem rather wasteful to parse a constant string to generate a initial value anyway! :laugh:
Hello,
Please try this

DateTime myDt = DateTime.ParseExact("1900-01-01", "yyyy-MM-dd",
                                       System.Globalization.CultureInfo.InvariantCulture)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900