Reputation: 3243
I have some code that is logging a timestamp in format from a thick client app
DateTime.UtcNow.ToString("MM/dd/yy HH:mm:ss")
Now, on a client running in China (not sure exactly which locale) this is producing a date in the log with the format
11-20-13 02:14:03
I notice it's using - instead of / to delimit the parts, even though I explicitly wanted /
I tried to set the current culture to Chinese simplified zh-CN but I wasn't able to reproduce how the remote client was able to produce that string
Does current culture locale affect the output of this format string? Or does / have some other meaning I'm not aware of?
Upvotes: 4
Views: 3866
Reputation: 125630
Yes, that's how it works. /
is being replaced with the local date separator. The same applies to :
as a time separator. You can find more on MSDN: Custom Date and Time Format Strings.
To change that, escape them with \
:
DateTime.UtcNow.ToString(@"MM\/dd\/yy HH\:mm\:ss")
Upvotes: 3
Reputation: 149020
Yes, the /
character is a placeholder for whatever the current culture uses to separate parts of the date. From MSDN:
The "/" custom format specifier represents the date separator, which is used to differentiate years, months, and days. The appropriate localized date separator is retrieved from the DateTimeFormatInfo.DateSeparator property of the current or specified culture.
As with other format specifiers, you can escape the /
with a \
:
DateTime.UtcNow.ToString(@"MM\/dd\/yy HH\:mm\:ss")
Or specify an explicit culture when formatting the string:
DateTime.UtcNow.ToString("MM/dd/yy HH:mm:ss", CultureInfo.InvariantCulture)
Upvotes: 5