Loren Pechtel
Loren Pechtel

Reputation: 9093

Windows is removing slashes from my dates on ONE machine

I have two date formats in my program. On one particular machine:

My code is in C#. The guy whose machine it is has some experience programming and was able to get the same sort of behavior with Delphi. Why are the forward slashes missing in the formatted dates? Or should I suggest he nuke and pave the offending box?

Upvotes: 2

Views: 282

Answers (1)

Michael Liu
Michael Liu

Reputation: 55499

In a custom date and time format string, an unquoted slash is just a placeholder for the culture-specific DateTimeFormatInfo.DateSeparator (which can be strings like "-" and ".", depending on the culture). If on that other machine, someone has customized the Windows region settings and removed the separators from the short date format, then DateTimeFormatInfo.DateSeparator will be an empty string, and your formatted dates will lack slashes.

To force the use of slashes, surround the slashes with quotes in your format strings, or pass CultureInfo.InvariantCulture to the formatting methods. For example, you could write

date.ToString("M'/'d'/'yyyy h:m:s tt")

or

date.ToString("M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture)

CultureInfo.InvariantCulture would be a better choice if you're expecting ":" and AM/PM in your formatted times instead of localized values like "." and Mo Mosong/Mo Maitseboeng (South Africa).

Upvotes: 9

Related Questions