Most Simple Way to Subtract a Day from DATE in Google Apps Script [duplicate]
Asked Answered
M

2

1

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)
}
Mindamindanao answered 8/11, 2022 at 1:29 Comment(1)
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://mcmap.net/q/80358/-calculate-the-date-yesterday-in-javascript 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.Royal
N
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);
Nakasuji answered 8/11, 2022 at 3:25 Comment(0)
R
1

I like this:

let dt = new Date();
let minusone = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-1)
Rehm answered 8/11, 2022 at 1:50 Comment(1)
thanks that helped a lot with this : https://mcmap.net/q/1448022/-custom-functions-and-recalculationBacterin

© 2022 - 2024 — McMap. All rights reserved.