-1

I want this day to be 25 days from now, and it needs to correspond with normal workday measurements. Saturdays, Sundays and Holidays need to be removed. I've tried using Google sheets and javascript:

Here is what I have.

function Calendar() {
  var now = new Date();
  var year = now.getFullYear();
  var month = new Array();
month[0] = "01";  
month[1] = "02";
month[2] = "03";
month[3] = "04";
month[4] = "05";
month[5] = "06";  
month[6] = "07";
month[7] = "08";
month[8] = "09";
month[9] = "10";
month[10] = "11";
month[11] = "12";  
  var monthnum = month[now.getMonth()];
  var weekday = new Array();
  weekday[0] = "Sunday";
  weekday[1] = "Monday";
  weekday[2] = "Tuesday";
  weekday[3] = "Wednesday";
  weekday[4] = "Thursday";
  weekday[5] = "Friday";
  weekday[6] = "Saturday";

  var day = weekday[now.getDay()];

  var date = now.getDate();

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Schedule");
  var last = sheet.getLastRow();
  var destination = sheet.getRange(last, 1);
  var format = sheet.getRange(10, 1, 1, 7);
  var insertdate = sheet.getRange(last, 2);
  var insertday = sheet.getRange(last, 1)



  sheet.insertRowAfter(last);
  sheet.deleteRow(7)
  insertdate.setValue(monthnum + "/" + date + "/" + year);
  insertday.setValue(day)


  Logger.log(monthnum + "/" + date + "/" + year + " " + day)
}
wahwahwah
  • 3,254
  • 1
  • 21
  • 40

1 Answers1

-1

There are many ways to do it, but the easily one is: 86400000 is one day

86400000= 24 Hours * 60 Min * 60 Seconds * 1000 Milliseconds

Here is an example which may help you:

var first = new Date();
var last = new Date(new Date(first).getTime() + (86400000 * 25));
var now = new Date(); now.setDate(now.getDate() + 25);
console.log(first);
console.log(last);
console.log(now);
ferhado
  • 2,363
  • 2
  • 12
  • 35
  • This one actually worked var now = new Date(); now.setDate(now.getDate() + 25); – BrendanMan33 Nov 01 '17 at 21:36
  • Disregard my last message. Using the code I posted worked. Give it a try – BrendanMan33 Nov 01 '17 at 21:38
  • Please give more details, then can I help you – ferhado Nov 01 '17 at 21:44
  • No this one // var now = new Date(); now.setDate(now.getDate() + 25); – BrendanMan33 Nov 01 '17 at 21:45
  • One day is not always 24 hours long where daylight saving is observed. You should add days, not time. See [*Add +1 to current date*](https://stackoverflow.com/questions/9989382/add-1-to-current-date), which gives the basic algorithm for adding or subtracting any number of days. – RobG Nov 01 '17 at 23:21
  • @RobG Go learn first mathematics not Programming, and see all the answers of your link, and read my answer carefully!!:) – ferhado Nov 04 '17 at 13:31
  • @FerhadOthman— if your intention is to show that adding ms does not necessarily give the same result as adding days (i.e. *last* is not equal to *now*), you should explain that in your answer. Otherwise you're depending on the example crossing a daylight saving boundary (it doesn't for me), that the OP will notice the difference and understand why. – RobG Nov 04 '17 at 23:46