I need a date string with one digit for the month, one digit for the day and two digits for the year (which is M/d/yy in the US, d/M/yy in most other places). I also need it to work across locales so it should automatically deal with the order of month/day and the separator. The closest to this is the d format which returns MM/dd/yyyy when called via date.ToString("d").
This needs to work across locales, so I tried this is what I came up with:
string format = culture.DateTimeFormat.ShortDatePattern
.Replace("yyyy", "yy")
.Replace("dd", "d")
.Replace("MM", "M");
return date.ToString(format);
Using culture.DateTimeFormat.ShortDatePattern gives me the date format used for the d date format. Using this property will take care of the ordering of day/month/year and the date separator.
Is there a better way to do this?
If you definitely want exactly the format
M/d/yythen just use that:However, that isn’t necessarily an appropriate string for the culture. For example, it would be rather alien to anyone in the UK, which would expect more of a
d/M/yyformat – day before month.Note that the
MM/dd/yyyyformat may be what you get when you usedate.ToString("d")but that’s definitely not what you’d get in the UK.Basically, you need to decide whether you want to nail down the format, in which case you’re blowing localization – or whether you want to use the “normal” culture-specific short date string format, in which case
date.ToString("d", culture)would make sense.It sounds like you may be trying to go half way – “the short date format, but definitely don’t use 4-digit years” etc. While that could work (make sure you pass
cultureinto theToStringmethod as well) there’s no guarantee that the culture’s short date pattern will be usingddetc to start with. It could go horribly wrong, for example if the normal short date format isddd dd MMM yyyyyou’d end up withdd d MM yy, so something like “09 9 06 12” for June 9th 2012.Basically, unless you’ve actually got a mapping from culture to “even shorter date format” I wouldn’t suggest fudging it like this.
EDIT: Here’s a short program whose output you should check. Basically it prints the original format and the modified one.
On my machine, scanning down the list quickly, I think you’d actually be okay. Nothing seems to use
dddorMMMin the original format. But there’s no guarantee that that will continue to be the case…