Reputation: 151
I had search for along, how to get a date in a cell with google apps script correcty, but I can not find. Please don't offer getDisplayValues()
, because date in a cell is in "dd.MM" format. I wrote a script. that takes the date in the cell, and then format it to "dd.MM.yy". But with the getValue()
it comes out sloppy in the case where I have a cell date 25.03.2019
, and after receiving the script, and formatting it looks 24.03.2019
.
Upvotes: 0
Views: 252
Reputation: 9571
Most likely, you have a time zone issue. You likely want the spreadsheet and the script to be using the same time zone. You can do that in the spreadsheet settings and in the project properties menu of the script.
Alternatively, you can get the spreadsheet time zone and use that in your Utilities.formatDate()
function.
function getAndFormatDate() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName("Sheet29");
var cell = sheet.getRange("A1");
var value = cell.getValue();
var timeZone = spreadsheet.getSpreadsheetTimeZone(); // Get spreadsheet time zone
var formattedDate = Utilities.formatDate(value, timeZone, "dd.MM.yy");
Logger.log(formattedDate);
}
Upvotes: 1