arithma wrotedd/MM/yyyy will be converted internally to something like dd.MM.yyyy IIRC if the server was in Germany, for example.
The DateTime.ToString("t") is what I was heavily referring to.
At any rate, I don't think the solution is to find a formatting string that works the same across different cultures, why the bother when you can be deterministic about it?
It's just a nitpick, but I hope it may be appreciated as a bug saving solution for down the drain issues (you really never know where your clients will host their sites and so on...)
Well, arithma, you are right. Glad I learned something about globlization. A slash is not considered a literal, it is a meta-character that represents the date separator defined in
System.Globalization.DateTimeFormatInfo.DateSeparator. To satisfy my curiosity, I searched for a way to format a DateTime as I intended originally, and here is the code for it:
private static string GetRelativeDateExpression(DateTime theDate)
{
string relative = string.Empty;
if (DateTime.Today == theDate.Date)
relative = "Today";
else if (DateTime.Today.AddDays(-1) == theDate.Date)
relative = "Yesterday";
else
relative = theDate.ToString("dd\\/MM\\/yyyy"); // or any other format
return string.Format("{0} at {1}", relative, theDate.ToString("hh\\:mm\\:ss tt"));
}
notice that I am escaping the slash with a back slash, same for ':' to be on the safe side.
Using CultureInfo.InvariantCulture would work the same way, but in the end whether you choose to format with a fixed formatting or a culture specific one depends on the requirements.
Just a side note: CultureInfo.InvariantCulture formats based on the English language (independent of a country) which in some sense is not culture neutral, from MSDN:
The CultureInfo.InvariantCulture property is neither a neutral nor a specific culture. It is a third type of culture that is culture-insensitive. It is associated with the English language but not with a country or region.