1

I have a script that places today's date (the date when the script is run) in various cells in a spreadsheet in Sydney time which looks like this:

cell.setValue("" + Utilities.formatDate(new Date(), "UTC+10", "dd-MM-YYYY"));

This works fine but there are also some cells where I need it to place not today's date but a few days or weeks in the future - eg some cells I need filled with a value that is 5 days from when the script is run, two weeks, etc. How would I go about doing this? None of the variations I can find online seem to work properly.

Thanks!

mm123
  • 19
  • 5

2 Answers2

1

To make Akshin's answer a little simpler...

var currdate = new Date();
var daystochange = 5;
var newdate = new Date(currdate.getFullYear(), currdate.getMonth, currdate.getDate() + daystochange);

Now keep in mind that if you want to make sure the new date in still inside the business week, you can use newdate.getDay() to get the day as an integer (0 = sunday, 1 = monday . . . 5 = friday, etc...). And you can always format the date as Akshin stated with

var formatteddate = Utilities.formatDate(newdate);

See more on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Harrison Gibbs
  • 143
  • 2
  • 9
0

Try this:

var nextDay = new Date()
  nextDay = nextDay.setDate(nextDay.getDate()+1);
  cell.setValue(Utilities.formatDate(new Date(nextDay), "UTC+10", "dd-MM-YYYY"));

Or

cell.setValue(Utilities.formatDate(new Date(new Date().setDate(new Date().getDate()+1)), "UTC+10", "dd-MM-YYYY"))
Akshin Jalilov
  • 1,658
  • 1
  • 12
  • 12