'Date Format from Google Sheets to HTML

I have the date as follows in Google Sheets

enter image description here

Now I use the below code to get the date and sent to Mail

function(){
const rs = SpreadsheetApp.getActiveSpreadsheet();
const ws = rs.getSheetByName("Page1");
const dateval=ws.getRange("C2").getValue();
const htmlTemplate=HtmlService.createTemplateFromFile("htmlsend");
htmlTemplate.dateval=dateval;
}

When I call the dateval to HTML and when I send to Mail, I get the output of date as Fri Sep 17 2021 00:00:00 GMT+0530 (India Standard Time) instead of 17-09-2021

Help me to convert the date format to 17-09-2021 as like



Solution 1:[1]

I would do it this way:

function(){
  const rs = SpreadsheetApp.getActiveSpreadsheet();
  const ws = rs.getSheetByName("Page1");
  const dateval=ws.getRange("C2").getValue();
  const htmlTemplate = HtmlService.createTemplateFromFile("htmlsend");

  htmlTemplate.dateval = Utilities.formatDate(dateval, 'America/Sao_Paulo', 'dd-MM-yyyy');
}

Where "America/Sao_Paulo" is the code of the timeZone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ludmilla