Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a DateTime.ToString format expression affected by the current culture locale?

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?

like image 214
blue18hutthutt Avatar asked Feb 14 '23 15:02

blue18hutthutt


1 Answers

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)
like image 86
p.s.w.g Avatar answered Feb 17 '23 11:02

p.s.w.g