Home > database >  Most Simple Way to Subtract a Day from DATE in Google Apps Script
Most Simple Way to Subtract a Day from DATE in Google Apps Script

Time:11-08

I see a bunch of confusing solutions around, and I'm convinced there's a more simple, elegant way.

Right now, "date" would print 11/07. I simply want it to print 11/06.

I've tried subtracting one all over, I understand it's a string now, but I thought I could subtract before I converted it?

Ultimately, I want to hide tabs that contain yesterday's date. The rest of my script works perfectly, just can't figure this part out.

function HideOldTabs(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var allsheets = ss.getSheets();
  var date = Utilities.formatDate(new Date(),"EST", "MM/dd")  

  var data = []

  for(var s in allsheets){
    var sheet = allsheets[s];
    if(
      //  (sheet.getName() == "Summary") || 
      //  (sheet.getName() == "Data") || 
      //  (sheet.getName() == "Sheet1") ||
       (sheet.getName().includes(date))
    ){

        sheet.hideSheet();
    }
  }
  return data;
  // console.log(date)
}

CodePudding user response:

I like this:

let dt = new Date();
let minusone = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-1)

CodePudding user response:

The Date class isn't exactly known for it's elegance (there is actually a proposal to replace it with a new API called Temporal). But for all it's faults you can decrement a date fairly easily.

let date = new Date();
date.setDate(date.getDate() - 1);
  • Related