0

I have a prompt that also displays some values to the user, one of them being a date. The problem is: this displays a too detailed date (ex.: Tue Mar 30 2021 06:29:23 GMT-0400 (Hora de verão oriental norte-americana)). How can I abbreviate this date only for the prompt?

Rubén
  • 29,320
  • 9
  • 61
  • 145

3 Answers3

3

An alternative approach would be to use the Utilities.formatDate function:

function displayPrompt() {
  const ss = SpreadsheetApp.getActive();
  const formattedDate = Utilities.formatDate(new Date(),ss.getSpreadsheetTimeZone(),"EE MMM dd y");
  const ui = SpreadsheetApp.getUi();
  const response = ui.prompt('Title', formattedDate, ui.ButtonSet.YES_NO);
}

This would give you this prompt window:

enter image description here

soMarios
  • 24,472
  • 8
  • 28
  • 43
2

If you need complete control of the output, use Utilities.formatDate, which returns a string:

var d = new Date();
var tz = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var formattedDate = Utilities.formatDate(d, tz, "yyyy-MM-dd");
Logger.log(formattedDate);

SpreadsheetApp.getActive().getSpreadsheetTimeZone() gives you the time zone for the parent spreadsheet, but you can alternatively hardcode a value from this list (e.g., "GMT").

Kate
  • 1,095
  • 9
  • 13
1

You can use 2 methods, toLocaleDateString() or toDateString() to shorten your date in Apps Script, as seen below:

function myFunction() {
   var d = new Date();
    var date = d.toLocaleDateString();
    var date2 = d.toDateString();

    Logger.log(date);
    Logger.log(date2);
}

These 2 methods will give you a shorten date format as seen here:

enter image description here

Irvin Jay G.
  • 2,267
  • 1
  • 2
  • 10