30 August, 2013

C# datetime formatting separators issue

Problem:
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy"));  
Output:
08-28-2013 
Expected output: 
08/28/2013

Solution:
Console.WriteLine(DateTime.Now.ToString("MM/dd/yyyy"CultureInfo.InvariantCulture));
 
Reason,
If a CultureInfo is not specified, the current culture will be used.
If this is a culture that doesn't use slashes as separators in dates and 
the format string specifies the date separator to be a /, that is replaced 
by whatever the actual culture date separator is 

No comments:

Post a Comment