I’ve been using custom formats for displaying DateTimes as found here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
e.g.
new DateTime(2011,10,5).ToString("dd");
Returns: "05"
and:
new DateTime(2011,10,5).ToString("d MM");
Returns "5 10"
Which are both as I would expect, but:
new DateTime(2011,10,5).ToString("d");
Returns: "05/10/2011"
When I would have expected it to return: "5"
This doesn’t matter in practise as you would use: new DateTime(2011,10,5).Day to get the day, but I am interested as to why the custom format didn’t work as I would have expected.
Try reading here Standard Date and Time Format Strings
"d" Short date pattern.. Yes, it was probably a little stupid to reuse thed:-), fortunately it’s a corner case.In general you should always use the
CultureInfo.InvariantCulture, for things that aren’t input/output to the user, unless you know EXACTLY what you are doing and you know your program won’t be sold abroad. So if you are showing a date to the user, use his culture/the local culture. If you are asking a date to the user the same. But if you are using the date to name a file or to do some internal operations, normally it’s better to useCultureInfo.InvariantCulture.Try this:
and tell me what you see 🙂