0

My older LINQ code used to have something like this:

DateOfBirth = string.Format("{0:MM/dd/yyyy}", myTable.DateOfBirth),

But now I wanted to be able to format that how ever user changes their date pattern from Windows, so I changed it to be like this and it works:

string regionShortDate = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
string temp = "{0:" + regionShortDate + "}";
DateOfBirth = string.Format(temp, myTable.DateOfBirth),

So notice I had to use string concatenations to build my string formatter? I just think there should be a more professional way of doing this. What do you suggest?

toad
  • 1,351
  • 8
  • 14

2 Answers2

1

Use DateTime.ToShortDateString Method

DateOfBirth = myTable.DateOfBirth.ToShortDateString();

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.

And

The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture. The return value is identical to the value returned by specifying the "d" standard DateTime format string with the ToString(String) method.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • Ok will try it now, thanks. But yesterday I had situations that my code was saying like DateTime.Now.ToString("MM/dd/yyyy") and instead I wanted to use that "ToShortDateString" and in that case it didn't work as expected. Not sure why tho –  Jan 24 '14 at 19:24
  • @DevWannaBe, `ToShortDateString` would return you the string based on client computer culture settings. It could be `MM/dd/yyyy` or `dd/MM/yyyy`, and that is what it seems you are trying to do using your code in the question – Habib Jan 24 '14 at 19:26
  • Oh Ok I read your post that explains it, see it says it is using "Culture" so like "en-US" or "en-CA"...but that's not what I want. In control panel regional settings you can still keep it in US but change the short date pattern to yy/MM/dd so it won't look at that. –  Jan 24 '14 at 19:26
  • yeah Culture its self depends on the Windows settings when we install it, but short date patterns that I need are not being honored by this method. –  Jan 24 '14 at 19:27
  • But your current code in question is referencing `CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern` , Is your current code working as expected ? – Habib Jan 24 '14 at 19:28
  • Yep it is working as expected. Looks like CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern is looking at the short date patterns that we pick from the drop down in RegionLanguage settings of control panel but the "Culture" that is like "en-US", etc.. that one is separate and it comes from windows installation. If you have access to VisualStudio give it a quick shot –  Jan 24 '14 at 19:30
  • 1
    @DevWannaBe, `ToShortDateString` would give you the same output as your code, I just tried in VS, selected `yy-MM-dd` as the format and DateTime.Now.ToShortDateString() returned `14-01-24` – Habib Jan 24 '14 at 19:42
  • Thanks, will test it now. One last question: my DateOfBirth is a null able type of DateTime so I should say .DateOfBirth.Value.ToShortDateString() ... do you think that is safe? assuming the older code didn't have a null exception so hopefully this should remain fine too? right? –  Jan 24 '14 at 19:48
  • 1
    @DevWannaBe, you need to put in place a check for `HasValue` before calling that method, otherwise you will get an invalid operation exception `Nullable object must have a value.` but not a NRE. – Habib Jan 24 '14 at 19:53
1

You can use ToShortDateString as Habib suggests.

It depends on CultureInfo.CurrentCulture because while CultureInfo holds a variety of information about matters that generally vary due to language and locality, but include personal preferences. Hence while for me while CurrentCulture.Name returns en-IE, DateTime.Now.ToShortDateString() returns 2014-01-24 rather than 24/01/2014 as one would get by using the object returned from CultureInfo.GetCultureInfo("en-IE"), because my own rig is set up to use en-IE for language and ISO 8601 for dates and times.

Therefore, don't worry about the name of the current culture and current UI culture relating solely to its language; they can have UseUserOverride set to true and actually be constructed from the user's settings.

As well as Habib's suggestion, if you want to use the current short date string (or that from any other CultureInfo you can use the string "d". This gives no advantage in your case where the short date string is all that is used, but it's useful if it will be part of a larger phrase:

string.Format("Today is {0:d}.", DateTime.Now)

(On my system, Today is 2014-01-24. on yours perhaps something else).

There are other useful single-string formatting strings for dates, that can be similarly used.

Edit:

Since you've just pointed out that you are using a DateTime? rather than a DateTime, then you've three options:

  1. Use DateOfBirth = string.Format("{0:d}", myTable.DateOfBirth). This results in an empty string for null cases.
  2. Test for nullity with string.Format, e.g. DateOfBirth = myTable.DateOfBirth.HasValue ? string.Format("Born on {0:d}.", myTable.DateOfBirth.Value) : "Date of birth unknown";
  3. Test for nullity with ToShortDateString; DateOfBirth = myTable.DateOfBirth.HasValue ? myTable.DateOfBirth.ToShortDateString() : "Date of birth unknown";.
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • 1
    @DevWannaBe, the reason you are not getting exception in case it if it null is because of String.Format, See this question http://stackoverflow.com/questions/13276418/how-string-format-handles-null-values – Habib Jan 24 '14 at 20:03
  • Thanks Jon for the detailed info. –  Jan 24 '14 at 20:12