John Smith
John Smith

Reputation: 397

Change dateformat from mm/dd/yy to dd/mm/yy

I'm using a Google Form to collect dates from users, they use a datepicker. The format is US with mm/dd/yy (1/25/2016), but i need EU format dd/mm/yy (25/1/2016).

The script collects serval dates:

  var Date1 = e.values[24]; 
  var Date2 = e.values[25];
  var Date3 = e.values[26];
  var Date4 = e.values[27];  
  var Date5 = e.values[28];

How can i convert these to EU dateformat?

This is what iv'e tried so far without success:

1)

  function myFunction() {
      var preDate = "1/25/2016";
      var postDate = preDate.Utilities.formatDate(dt, "PST", "dd/mm/yy");
      Logger.log(postDate);
    }

Error: TypeError: Cannot call method "formatDate" of undefined.

2)

function myFunction() {
  var preDate = "1/25/2016"; 
  var postDate = Date.parseExact(preDate ,"dd/MM/yyyy")
  Logger.log(postDate);
}

Error: TypeError: Cannot find function parseExact in object function Date() { [native code for Date.Date, arity=1] }

Upvotes: 0

Views: 1830

Answers (2)

Amit Agarwal
Amit Agarwal

Reputation: 11268

If you have the dates in a Google Spreadsheet, you can use the Utilities.formatDate method to convert dates into another format.

function convertDate() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var source = sheet.getRange("A1").getValue();
  if (typeof source === "date") {
   sheet.getRange("B1").setValue(Utilities.formatDate(dt, "PST", "dd/mm/yy"));
  }
}

An easier option would be that you select the entire column in the spreadsheet and the change the default format under the Format menu (see screenshot).

Change Date Format in Spreadsheet

Upvotes: 2

B.Karaca
B.Karaca

Reputation: 19

You can use DateJS for that, like this:

http://code.google.com/p/datejs/wiki/APIDocumentation#parseExact

Date.parseExact("10/15/2004", "M/d/yyyy");  // The Date of 15-Oct-2004
Date.parse("15-Oct-2004", "d-MMM-yyyy");    // The Date of 15-Oct-2004
Date.parse("2004.10.15", "yyyy.MM.dd");     // The Date of 15-Oct-2004
Date.parseExact("10/15/2004", ["M/d/yyyy", "MMMM d, yyyy"]); // The Date of 15-Oct-2004

In your question you need that:

Date.parseExact("dateyouneed" ,"dd/MM/yyyy")

Upvotes: 1

Related Questions