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?
Asked
Active
Viewed 297 times
0
Rubén
- 34,714
- 9
- 70
- 166
Django Unchained
- 105
- 1
- 1
- 7
3 Answers
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:
Marios
- 26,333
- 8
- 32
- 52
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,263
- 10
- 13
0
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:
SputnikDrunk2
- 3,398
- 1
- 5
- 17

