Home > Software engineering >  Apps Script: How to convert English date format to German date format?
Apps Script: How to convert English date format to German date format?

Time:10-19

I am pretty sure this is a small issue, but I just can´t figure out how to solve it, as I am new at using Apps script :(.

ISSUE:

I have a sheets consisting of 2 columns: A and B. The column A contains dates written in German format (Day/Month/Year). Example: October 18th, 2021 is written 18/10/2021. Now all I want to do is increment each day from column A by 1 day and write the outcome (so, the incremented days) in column B.

ATTEMPTED SOLUTION:

I´ve figured out how to increment the days and write the outcome in column B. But I am struggling to add the second part of my script, so that the incremented dates are written in German in column B (See scripts below).

QUESTION:

How and where can I include the “Utilities.formatDate()” into my code, so that the dates in column B are formatted in German? (So, DAY/MONTH/YEAR instead of MONTH/DAY/YEAR)?

Thank you so much in advance for your help.

// Increment dates in colum A by 1 day and write this incremented date in colum B
function incrementDateInGermanFormat() {
  var ss = SpreadsheetApp.getActiveSheet();
  var date = new Date(ss.getRange(1, 1).getValue());
  ss.getRange(1, 2).setValue(new Date(date.setDate(date.getDate()   1)));
}

// This is the line I´m having trouble to include in the script above
Utilities.formatDate(date, Session.getScriptTimeZone(), "dd/MM/yyyy")

CodePudding user response:

It is bad practice to convert date to strings. In this case, a better alternative is to simply change column B's date format to german in the spreadsheet user interface or use .setNumberFormat("dd/mm/yyyy") on column B.

  • Related