title: How to maintain date format in export to CSV in Google Apps Script
body: <p>I'm trying to export a Google Spreadsheet to CSV using the below function in Google Apps Script. Is it possible to maintain the date formatting in the CSV?</p>
  
  <p>If I use the menu option instead to download as CSV, the date formatting is maintained.</p>
  
  <p>I'm using the following function to export to CSV:</p>
  
  <pre><code>function exportToCSV(file) {
  
    // Get the selected range in the spreadsheet
    var ws = SpreadsheetApp.openById(file.getId()).getSheets()[0];
    var range = ws.getRange(1,1,ws.getLastRow(),ws.getLastColumn())
  
    try {
      var data = range.getValues();
  
      var csvFile = undefined;
  
      // Loop through the data in the range and build a string with the CSV data
      if (data.length &gt; 1) {
        var csv = "";
        for (var row = 0; row &lt; data.length; row++) {
          for (var col = 0; col &lt; data[row].length; col++) {
            if (data[row][col].toString().indexOf(",") != -1) {
              data[row][col] = "\"" + data[row][col] + "\"";
            }
          }
  
          // Join each row's columns
          // Add a carriage return to end of each row, except for the last one
          if (row &lt; data.length-1) {
            csv += data[row].join(",") + "\r\n";
          }
          else {
            csv += data[row];
          }
        }
        csvFile = csv;
  
      }
    }
    catch(err) {
      Logger.log(err);
      Browser.msgBox(err);
    }
  
    return csvFile;
  
  }
  </code></pre>