user2740190
user2740190

Reputation:

Changing format of String.Format parameters at run time

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?

Upvotes: 0

Views: 599

Answers (2)

Jon Hanna
Jon Hanna

Reputation: 113232

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";.

Upvotes: 1

Habib
Habib

Reputation: 223197

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.

Upvotes: 1

Related Questions