Reputation: 9093
I have two date formats in my program. On one particular machine:
date.ToString("M/d/yyyy h:m:s tt")
is producing things like 11222013 12:0:0 AM
. (The :0:0
is acceptable; it's a file that will normally not be read by a human.)date.ToString("MM/dd/yyyy hh:mm tt")
is producing things like 11222013 12:00 AM
.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
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