1

I have a problem with Google Script. I am trying to calculate all dates of a week. I have the Monday Value in a sheet and copy it to an array and then declare it as a variable. I can afterwards format it and extract the week, the year and so on.

However, if I want to add one day for Tuesday he is giving me the Monday Date and adding a 1 at the end.

I am using the following code:

var StartDatum = Data [5][7]
var Week = Utilities.formatDate(StartDatum, "GMT", "ww")-1;
var Month =Utilities.formatDate(StartDatum, "GMT", "MM");

for (var i=14; i < 21; i++){
var Datum = ((StartDatum + (i - 14)));
}

For StartDatum I receive a proper date:

Mon Jan 13 2020 09:00:00 GMT+0100 (CET)

Week and Month also work.

But for Datum I receive:

Mon Jan 13 2020 09:00:00 GMT+0100 (CET)0
Mon Jan 13 2020 09:00:00 GMT+0100 (CET)1
Mon Jan 13 2020 09:00:00 GMT+0100 (CET)2
Mon Jan 13 2020 09:00:00 GMT+0100 (CET)3

... and so one.

Could somebody please give me a hint about my mistake

  • Does this answer your question? [Google App Script Adding one Day to a date](https://stackoverflow.com/questions/40893620/google-app-script-adding-one-day-to-a-date) – ross Jan 21 '20 at 21:15
  • It seems the `+` in `StartDatum + ` is treated as string concatenation, so *StartDatum* is converted to a string. Then the result of `(i - 14)` is appended as a string too. You can't add a day to a Date object by just adding 1. – RobG Jan 21 '20 at 22:01
  • PS In javascript, variables starting with a capital letter are, by convention, reserved for constructors so *startDatum*, *datum*, etc. :-) – RobG Jan 21 '20 at 22:03

1 Answers1

0

When performing date manipulations, you need to use the get...() and set...() methods of the Date object.

Here's an example of starting with today's date and manipulating the Date object to depict the days of the following two weeks (which also happens to demonstrate clean handling of month boundaries):

Code:

function dateManipulationExample() {
  var timestamp = new Date();
  Logger.log('Starting date: ' + Utilities.formatDate(timestamp, 'UTC', 'yyyy-MM-dd'));

  for (var i = 1; i <= 14; i++) {
    timestamp.setDate(timestamp.getDate() + 1);
    Logger.log(i + ' day(s) later: ' + Utilities.formatDate(timestamp, 'UTC', 'yyyy-MM-dd'));
  }
}

Logging output:

[20-01-21 14:34:17:065 PST] Starting date: 2020-01-21
[20-01-21 14:34:17:066 PST] 1 day(s) later: 2020-01-22
[20-01-21 14:34:17:067 PST] 2 day(s) later: 2020-01-23
[20-01-21 14:34:17:067 PST] 3 day(s) later: 2020-01-24
[20-01-21 14:34:17:068 PST] 4 day(s) later: 2020-01-25
[20-01-21 14:34:17:068 PST] 5 day(s) later: 2020-01-26
[20-01-21 14:34:17:069 PST] 6 day(s) later: 2020-01-27
[20-01-21 14:34:17:070 PST] 7 day(s) later: 2020-01-28
[20-01-21 14:34:17:070 PST] 8 day(s) later: 2020-01-29
[20-01-21 14:34:17:071 PST] 9 day(s) later: 2020-01-30
[20-01-21 14:34:17:072 PST] 10 day(s) later: 2020-01-31
[20-01-21 14:34:17:072 PST] 11 day(s) later: 2020-02-01
[20-01-21 14:34:17:073 PST] 12 day(s) later: 2020-02-02
[20-01-21 14:34:17:073 PST] 13 day(s) later: 2020-02-03
[20-01-21 14:34:17:074 PST] 14 day(s) later: 2020-02-04

For reference, date math is briefly discussed in the Apps Script documentation.

chuckx
  • 6,484
  • 1
  • 22
  • 23
  • 1
    Setting the offset to UTC may have unexpected results early in the day for users with a positive timezone offset and late in the day for those with a negative offset. – RobG Jan 22 '20 at 04:41
  • Hello chuckx thank you for the code. I adapted mine based on your proposal and it worked. – Thorsten Liewald Jan 22 '20 at 09:16