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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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

2 Comments

@DevWannaBe, the reason you are not getting exception in case it if it null is because of String.Format, See this question stackoverflow.com/questions/13276418/…
Thanks Jon for the detailed info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.