I experiencing a strange behavior of C#. Its some thing like this..
var date = DateTime.Now.ToString("MM/dd/yyyy");
I expecting out to be
04/24/2009
but in actuall its returning
04-24-2009
and my OS culture is en-GB, I'm using .Net 3.5 and WPF
any solutions please... ???
A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime. ToString using the 'z' format specifier, which will include a local time zone offset in the output.
A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.
According to the MSDN docs for custom date and time format strings, /
is a placeholder:
Represents the date separator defined in the current DateTimeFormatInfo.DateSeparator property. This separator is used to differentiate years, months, and days.
If you want a definite slash, use "MM'/'dd'/'yyyy":
DateTime.Now.ToString("MM'/'dd'/'yyyy")
It uses the separator set up in the regional settings, since "/" is the substitute character for the separator.
You can create your own DateTimeFormat instance with different separators.
Try calling ToString on the date itself and passing the CultureInfo.InvariantCulture object:
string date = yourDate.ToString("MM/dd/yyyy", 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