0

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)
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
Anthony Madle
  • 371
  • 1
  • 10
  • About `Most Simple Way to Subtract a Day from DATE in Google Apps Script`, I thought that this thread will be the answer to your question. https://stackoverflow.com/q/5511323 About `Ultimately, I want to hide tabs that contain yesterday's date. `, I think that when this thread is used, your goal can be achieved. But, in your script, `data` is always `[]`. So, if you have any other questions, please tell me. If your question is only to retrieve yesterday's date, I will flag it as a duplicated question. – Tanaike Nov 08 '22 at 01:38

2 Answers2

2

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);
TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30
0

I like this:

let dt = new Date();
let minusone = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-1)
Cooper
  • 59,616
  • 6
  • 23
  • 54