why does DateTime.ToString("dd/MM/yyyy") give me dd-MM-yyyy?
I want my datetime to be converted to a string that is in format "dd/MM/yyyy"
Whenever I convert it using DateTime.ToString("dd/MM/yyyy")
, I get dd-MM-yyyy
instead.
Is there some sort of culture info that I have to set?
Solution 1:
Slash is a date delimiter, so that will use the current culture date delimiter.
If you want to hard-code it to always use slash, you can do something like this:
DateTime.ToString("dd'/'MM'/'yyyy")
Solution 2:
Pass CultureInfo.InvariantCulture as the second parameter of DateTime, it will return the string as what you want, even a very special format:
DateTime.Now.ToString("dd|MM|yyyy", CultureInfo.InvariantCulture)
will return: 28|02|2014
Solution 3:
Add CultureInfo.InvariantCulture
as an argument:
using System.Globalization;
...
var dateTime = new DateTime(2016,8,16);
dateTime.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Will return:
"16/08/2016"