Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Interesting thread !</p> <p>I have an application which reads in an Excel file, and lets the user export it's data to a .csv file in a <em>user-defined format</em>. </p> <p>I specifically wanted to give the users the ability to export dates to (exactly) a format like yyyy/mm/dd, regardless of their laptop's culture info.</p> <p>I tried a few of these suggestions, but the only one which did work was Pop Catalin's suggestion, to wrap any / characters with apostrophes:</p> <pre><code>outputFormat = outputFormat.Replace("/", "'/'"); valueToExport = ImportedDate.ToString(outputFormat); </code></pre> <p>This seems to be the <em>only</em> way to force ToString() to use the exact format string, without trying to do anything Culture specific.</p> <p><strong>Update</strong></p> <p>I always find it amusing when I answer a StackOverflow question, then hit the same bug a few years later, and stumble across my own answer !</p> <p>As mentioned in lots of these answers, the solution is to wrap forward-slash characters in apostrophes, to force <code>ToString()</code> to ignore the culture settings.</p> <p>So, if your Culture settings have a full-stop character as the date separator character (eg "<code>20.07.2015</code>"), then here's what you'd see, when attempting to nicely format the date of Christmas Day, and how you can easily force it to <em>always</em> use forward-slashes:</p> <pre><code>DateTime dtChristmas = new DateTime(2015, 12, 31); // This might return "31/12/2015", "31.12.2015", etc, depending on Culture settings string str1 = dtChristmas.ToString("dd/MM/yyyy"); // ...but these two statements will both *always* return "31/12/2015" string str2 = dtChristmas.ToString("dd'/'MM'/'yyyy"); string str3 = dtChristmas.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload