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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With