I'm just wondering why this code
DateTime.Now.ToString("MM/dd/yyyy"); and String.Format("{0: MM/dd/yyyy}", DateTime.Now);
both returns 03 31 2016 instead of 03/31/2016.
/
is a format specifier which is transformed to your localized date separator. Either wrap it in apostrophes or use the overload with DateTimeFormatInfo.InvariantInfo
.
Console.WriteLine( DateTime.Now.ToString("MM'/'dd'/'yyyy") );
Console.WriteLine( DateTime.Now.ToString("MM/dd/yyyy", DateTimeFormatInfo.InvariantInfo) );
or with String.Format
:
Console.WriteLine( String.Format("{0:MM'/'dd'/'yyyy}", DateTime.Now) );
Console.WriteLine( String.Format(DateTimeFormatInfo.InvariantInfo, "{0:MM/dd/yyyy}", DateTime.Now) );
You can also use CultureInfo.InvariantCulture
instead of DateTimeFormatInfo.InvariantInfo
.
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