So I have been adding a DateTime.Now.ToString("MM/DD/YYYY")
to a List (along with a bunch of other data) and the later writing these lists to individual rows in an Excel workbook.
This all works great (it's something I do regularly), except, the month. I have tried exporting it three times and each time I get a different month;
the first time 56/26-17,
the second 2/26/2017,
and the third 14/26/2017....
Use this instead:
DateTime.Now.ToString("MM/dd/yyyy")
"MM" for month. "dd" for days. "yyyy" for year.
"MM/DD/YYYY" is wrong format:
Console.WriteLine(DateTime.Now.ToString("MM/DD/YYYY")) // prints "06/DD/YYYY"
DateTime.Now.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
As already answered you need to have format specifier for day and year in small letters but as an add on you should also specify the culture variable to make sure that final output contains "/" between date parts. Without this culture parameter "/" could be replace by the date separator of the culture of system where code is running like "-".