Google Script looping through days issue - javascript

My first post here so go easy.
I am trying to create a Google script on a spreadsheet which uses a form to get information about events which are then added to a calendar. The script creates a createEventSeries for selected weekdays between two dates. It also checks how many events there are on valid days for every hour between those the events times.
I am having issues with one line of code which should add a day on each loop:
var floatdate = new Date(floatdate.setDate(starttime.getDate()+d)) ;
Where floatdate is the date which it is currently being checked, d is in a number days after starttime.
my issues is that when d = 4 the day and month changes, see this example:
var floatdate = new Date(floatdate.setDate(starttime.getDate()+0))
Logger.log(floatdate)
var floatdate = new Date(floatdate.setDate(starttime.getDate()+1))
Logger.log(floatdate)
var floatdate = new Date(floatdate.setDate(starttime.getDate()+2))
Logger.log(floatdate)
var floatdate = new Date(floatdate.setDate(starttime.getDate()+3))
Logger.log(floatdate)
var floatdate = new Date(floatdate.setDate(starttime.getDate()+4))
Logger.log(floatdate)
Gives the following:
[17-06-02 05:06:47:933 PDT] Mon May 29 08:00:00 GMT+08:00 2017
[17-06-02 05:06:47:933 PDT] Tue May 30 08:00:00 GMT+08:00 2017
[17-06-02 05:06:47:934 PDT] Wed May 31 08:00:00 GMT+08:00 2017
[17-06-02 05:06:47:935 PDT] Thu Jun 01 08:00:00 GMT+08:00 2017
[17-06-02 05:06:47:935 PDT] Mon Jul 03 08:00:00 GMT+08:00 2017
Please help!

I had the same issue with nonsence date iteration in log view:
[20-06-25 20:42:05:887 CEST] Month 2020-06-26T18:42:05+0000
[20-06-25 20:42:05:889 CEST] Month 2020-06-27T18:42:05+0000
[20-06-25 20:42:05:893 CEST] Month 2020-06-29T18:42:05+0000
[20-06-25 20:42:05:900 CEST] Month 2020-07-03T18:42:05+0000
At the end, I found it is just some weird sample and whole output can be found in
"Apps Script Dashboard" (View -> Logs -> link Apps Script Dashboard). This whole output is correct with all dates.
Another option how to get to whole Log outputs is https://script.google.com/home/my -> select your project -> project details menu -> select Executions -> open your execution and observe results.

Related

GSuite services - Calendar - bug with timezone and years?

I'm playing with the GSuite services Calendar (https://developers.google.com/apps-script/reference/calendar/calendar) to retrieve events from my Google Calendar.
I'm french (this is important for timezone).
I noticed something strange :
On my Google Calendar, I create an event, for exemple: 2017-11-17 from 10am to 18pm (notice the year 2017).
With Javascript and calendar service API, I retrieve this event and print start and end date:
function runMe() {
var calendar = CalendarApp.getDefaultCalendar();
Logger.log('My default calendar is set to the time zone "%s".', calendar.getTimeZone());
const events = calendar.getEvents(new Date(2017, 10, 17), new Date(2017, 10, 18));
events.forEach(event => Logger.log (event.getStartTime()+" - "+event.getEndTime()));
}
// logs:
// My default calendar is set to the time zone "Europe/Paris".
// Fri Nov 17 2017 09:00:00 GMT+0100 (heure normale d’Europe centrale) - Fri Nov 17 2017 17:00:00 GMT+0100 (heure normale d’Europe centrale)
The date is not correct, I expect Fri Nov 17 2017 10:00:00 GMT+0100 (heure normale d’Europe centrale) - Fri Nov 17 2017 18:00:00 GMT+0100 (heure normale d’Europe centrale)
Now, on my Google Calendar, I create another event at the same date, but on year 2018.
With Javascript and calendar service API, I retrieve this event and print start and end date:
...
function runMe() {
var calendar = CalendarApp.getDefaultCalendar();
Logger.log('My default calendar is set to the time zone "%s".', calendar.getTimeZone());
const events = calendar.getEvents(new Date(2018, 10, 17), new Date(2018, 10, 18));
events.forEach(event => Logger.log (event.getStartTime()+" - "+event.getEndTime()));
}
// logs:
// My default calendar is set to the time zone "Europe/Paris".
// Sat Nov 17 2018 10:00:00 GMT+0100 (heure normale d’Europe centrale) - Sat Nov 17 2018 18:00:00 GMT+0100 (heure normale d’Europe centrale)
The date is correct.
What happened?
This appears to be a bug!
I've done a lot of testing regarding this and it appears that the Calendar API returns the time incorrectly between the period between when DST ended in 2017, and when it began again in 2018.
I have taken the liberty of reporting this on Google's Issue Tracker for you, detailing the behaviour:
Calendar API returns incorrect event start and end time for events between end of DST 2017 and beginning of DST 2018
You can hit the ☆ next to the issue number in the top left on the page which lets Google know more people are encountering this and so it is more likely to be seen to faster.

Google Script - Wrong month in date

I have a problem with the date recognition of Google Apps Script. I have a code where I need to add days to a given date. However, the code returns the wrong month (higher not lower what would be a typical GAS problem). I wrote the following code to test the behaviour:
var datumStart = new Date();
datumStart = arrayTimesheet[5][7];
var datum2 = new Date();
datum2.setDate(datumStart.getDate());
and I receive the following results for the two variables:
datumStart Date Mon Feb 17 2020 09:00:00 GMT+0100 (Central European Standard Time)
datum2 Date Tue Mar 17 2020 22:50:39 GMT+0100 (Central European Standard Time)
the Array[5][7] is the value of a Google Sheets cell with the value 2020-17-02.
This datum2 variable is just a test variable to understand the error.
Has anybody an idea what is going wrong?
Javascripts getDate() and setDate() functions get and set the Day of the month.
So, your code is behaving as expected, you are instantiating the date datum2 in March, so it is a date in March (current date/time), later, you use setDate() to set it to the 17th of March.
To add days to a date, you can instantiate a new date using values from your existing date, plus the additional days, eg here we are getting a date 5 days in the future:
var future_day = new Date(datumStart.getFullYear(),datumStart.getMonth(),datum.getDate()+5);
Note that Javascript's underlying Date logic is solid, if your number of days crosses a month or year boundary, the results will be correct. (for example, if you add 31 days, you'll get the correct date in the following month)

moment.js - subtract 1 day from Sunday not working as expected

I have a moment object that I want to subtract 1 day from. The original date shows as Sun Jul 15 2018 12:00:00 and I want to subtract 1 day from it so that it outputs as Sat Jul 14 2018 12:00:00.
This seems like it should be really easy if I use the subtract() function, but it's changing the date to the upcoming Saturday, not the Saturday before July 15. I'm assuming this has something to do with the week starting on July 15.
This seems to only be an issue when I'm using Sunday as my starting date. How can I make this work the way I need it to?
Here is my JS:
var timeFormat = 'dddd h:mma';
var originalDate = moment("sunday 12:00:00pm", timeFormat);
var previousDay = moment(originalDate).subtract(1, 'days').format(timeFormat);
var newDate = moment(previousDay+"12:00:00pm", timeFormat);
$(".openTime span").text(originalDate);
$(".newOpenTime span").text(newDate);
This outputs Sun Jul 15 2018 12:00:00 as the originalDate and Sat Jul 21 2018 12:00:00 as the date subtracted by 1 day. As you can see the new date is now Sat Jul 21 for some reason.
Here's a JSFiddle link: https://jsfiddle.net/dmcgrew/b5ev8knd/22/
The problem is that you use a formatted string to create the newDate. The string says something like this:
'Saturday 12:00:00pm'. MomentJs has no information what Saturday you actually mean, so it just takes the next one, which is the 21st of July.
If you just use the previousDay moment and format it, it works:
https://jsfiddle.net/b5ev8knd/36/

JavaScript Date Bug February 2014

So I have this JS-code:
var d1 = new Date();
d1.setFullYear(2014);
d1.setMonth(1);
d1.setDate(1);
Should be Feb-01-2014, right? Only it's not... It returns Mar-01-2014 (actually, the full value is "Sat Mar 01 2014 20:54:29 GMT+0100 (Romance Standard Time)"). What the hell? Same thing happens with any other date value.
If I use this code, however, it works fine:
var d1 = new Date(2014, 1, 1, 0, 0, 0, 0);
The result is: Sat Feb 01 2014 00:00:00 GMT+0100 (Romance Standard Time)
Any ideas what's going on?
Here's what's happening, line for line:
You create a new date object with today's date.
var d1 = new Date(); // d1 = 2014-04-30
Then you set the year to 2014, which it already is, so nothing really happens.
d1.setFullYear(2014); // d1 = 2014-04-30
Here's the tricky part, because now you change the month to February. But this would make the date February the 30th (2014-02-30) which doesn't exist, so the JavaScript will try to find the closest valid date which is first of March (2014-03-01).
d1.setMonth(1); // d1 = 2014-02-30 is not valid so JS makes it 2014-03-01
Then you set the day to the first day of the month, which it already is, so nothing really happens here either.
d1.setDate(1) // d1 = 2014-03-01
You need to call setDate first. Basically it's grabbing the month and using the current date and since February doesn't have a 30th, it's defaulting to March.
Better to initialize Date, rather than have it default to the current date.
var d1 = new Date(0); // 1 January 1970 00:00:00 UTC
Try this:
d1.setFullYear(2014);
d1.setDate(1);
d1.setMonth(1);
What you were doing:
d1.setFullYear(2014); // change year to 2014 (30 Apr 2014 -> 30 Apr 2014)
d1.setMonth(1); // change month to 1 (30 Apr 2014 -> 30 Feb 2014, really 2 Mar 2014)
d1.setDate(1); // change day of month to 1 (2 Mar 2014 -> 1 Mar 2014)
By setting the date first, you're changing the date to 1 Apr 2014 before changing the month.

Add hours to javascript date object bug?

Hi guys so I've a wierd bug that I can't figure out. I create a date object from a date chooser and a set of combos with hours/mins.
Now the problem is when it's March 30th 2013 and 1pm adding 12 hours only adds 11 for some reason. With any other day like March 31st 2013 at 1pm this is fine. See example below
var d = new Date(1364601600000)
d.setHours(13)
d.setMinutes(13)
console.log(d)
d.setHours(d.getHours() + 12)
console.log(d)
console.log('--')
var d2 = new Date(1364688000000)
d2.setHours(13)
d2.setMinutes(13)
console.log(d2)
d2.setHours(d2.getHours() + 12)
console.log(d2)
See an example: http://jsfiddle.net/k8L2W/2/
The daylight scheme for the year 2013 works as follows :
Sunday, 31 March 2013, 01:00:00 clocks are turned forward 1 hour to
Sunday, 31 March 2013, 02:00:00 local daylight time instead
For reference, please check here

Categories

Resources