I'm trying to set a const "Yesterday" but the script is not recognised as a function (I'm using Google App Scripts). I've tried different syntax including:
const yesterday = today.setDate(-1);
and
const yesterday = new Date(today.setDate(-1));
But neither worked. I'm pretty sure it should be a minor change but I cannot figure out how to solve this. A little help would be highly appreciated, thanks !
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('My Report');
function insertColumn() {
const range = sheet.getRange('H1:H69').getValues();
const newrange = sheet.getRange('I1:I69');
const rules = sheet.getConditionalFormatRules();
const today = Utilities.formatDate(new Date(), "GMT 7", "MM/dd/yyyy");
const yesterday = new Date(today.setDate(-1));
CodePudding user response:
getYesterday() {
let date = new Date();
let yesterday_milliseconds = date.getTime() - 1000 * 60 * 60 * 24;
let yesterday = new Date();
yesterday.setTime(yesterday_milliseconds);
let strYear = yesterday.getFullYear();
let strDay = yesterday.getDate();
let strMonth = yesterday.getMonth() 1;
if (strMonth < 10) {
strMonth = "0" strMonth;
}
if (strDay < 10) {
strDay = "0" strDay;
}
return strYear "-" strMonth "-" strDay;
},
CodePudding user response:
function yesterday() {
Logger.log(new Date(new Date().setDate(new Date().getDate() - 1)));
}
From MDN setDate() returns: The number of milliseconds between 1 January 1970 00:00:00 UTC and the given date... not a date object
CodePudding user response:
You're not actually defining a function, you're creating a constant based on the value of today
. You should use something like:
const yesterday = function() {
today = new Date();
return new Date(today.setDate(today.getDate() - 1));
}
console.log(yesterday())