1

I am trying to subtract days in google apps script following the solution from this post: Trying to subtract 5 days from a defined date - Google App Script

However, the year does not deduct by 1 if we minus one day from the first day of the year. Below are my code:

var StartDate = new Date(ResponceDetails[C3StartDate]);
var StartYear = StartDate.getYear();
var StartMonth = StartDate.getMonth();
var StartDay = StartDate.getDate();
var tempEndDate = new Date(StartYear+1, StartMonth, StartDay);
Logger.log(StartDate);
Logger.log(tempEndDate);
var EndDate = Utilities.formatDate(new Date(tempEndDate.getTime()-(24*3600*1000)),"GMT+8","MM/dd/YYYY");
Logger.log(EndDate);

The purpose of this code is to calculate the due date of user after 1 year (including start day). So it has to be plus one year and minus by 1 day. Does the code above ok or anyone having a similar problem?

Thank you

Yeo
  • 41
  • 6
  • 1
    did not attempt StartDate.getTime()+364*(24*3600*1000) as to avoid complex checking of leap years where it has 366 days. Any better solution is appreciated, thanks! – Yeo Jan 01 '20 at 16:43

3 Answers3

2

Solved my problem! the year YYYY has to be small letters yyyy in the Utilities.formatDate(...)

The updated code:

var StartDate = new Date(ResponceDetails[C3StartDate]);
var StartYear = StartDate.getYear();
var StartMonth = StartDate.getMonth();
var StartDay = StartDate.getDate();
var EndDate = Utilities.formatDate(new Date(StartYear+1, StartMonth, StartDay-1), "GMT+8", "MM/dd/yyyy");
but any better ways to do this is very appreciated. and if anyone have a good document or references on the date format explaining the details of Date Format is really helps. Many thanks!
Yeo
  • 41
  • 6
  • 1
    The way you're setting it, i.e. year, month and day all in one go, is the best. Doing them one at a time can have unexpected results. However, you could do `let endDate = new Date(startDate.getFullYear()+1, startDate.getMonth(), startDate.getDate() -1)` to save a few lines of code. :-) – RobG Jan 02 '20 at 05:02
  • Thanks for the comment! Made the change to my code and works well, this help to reduce some code lines. – Yeo Jan 06 '20 at 02:42
1
function calcDate() {
  var date=new Date();
  var dueDate=new Date(date.getFullYear()+1,date.getMonth(),date.getDate()-1);
  Logger.log(Utilities.formatDate(date, Session.getScriptTimeZone(), "yyyy/MM/dd"));
  Logger.log(Utilities.formatDate(dueDate, Session.getScriptTimeZone(), "yyyy/MM/dd"));
  var tomorrow=new Date(date.getFullYear(),date.getMonth(),date.getDate()+1);
  Logger.log(Utilities.formatDate(tomorrow, Session.getScriptTimeZone(), "yyyy/MM/dd"));
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks for the reply! This help to reduce some code lines. Would like to check what is the differences between capital YYYY and yyyy representing in Utilities.formatDate? i tried also on mm/MM and dd/DD both representing different numbers – Yeo Jan 06 '20 at 02:47
  • 1
    [Simple Date Format](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – Cooper Jan 06 '20 at 02:59
  • Thanks,it should mean 'Y' representing week year, i was confuse previously what is week year was, but i think i got the meaning. It increment the year number after 52 weeks a year, so meaning usually it will have differences at the early and end of each year compare with usual year number. For example: https://www.epochconverter.com/weeks/2019 , 31Dec2018 for this calendar has already became year 2019 if we set using 'YYYY' – Yeo Jan 06 '20 at 05:21
0

I prefer using the JS getTime() method. It returns the number of milliseconds since 1/1/1970.

You can then add/subtract time in milliseconds.

function add_A_Day() {
  var StartDate = new Date(ResponceDetails[C3StartDate]).getTime();
  var EndDate = StartDate + 24 * 60 * 60 * 1000; // One day in milliseconds
  EndDate = new Date(EndDate);
  EndDate = Utilities.formatDate(EndDate, 'GMT+8', 'MM/dd/yyyy');
  Logger.log(EndDate);
}
ADW
  • 4,177
  • 1
  • 14
  • 22
  • Adding 8.64e7 milliseconds is not a good way to add 1 day as not all days are 24 hours long where daylight saving is observed. See [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date) The way the OP is doing it is much better. – RobG Jan 01 '20 at 19:55