1

I have this code to add a reminder to a calendar

function addCalendarEvent(eventDate, eventTitle){
  let dateObj = new Date(eventDate);
  let calendarId = "me@gmail.com";
  let cal = CalendarApp.getCalendarById(calendarId);
  let event = cal.createAllDayEvent(eventTitle, dateObj)
  event.addEmailReminder(420)

}

The eventDate is passed into the function as a string in the format dd/MM/YYYY but the output from let dateObj = new Date(eventDate); is in american format. ie. 02/10/2020 comes out as Mon Feb 10 2020 00:00:00 GMT+0000 (Greenwich Mean Time). Any help correcting this would be great. Dates give me an absolute headache

Chris Barrett
  • 571
  • 4
  • 23
  • Please indicate the format of `eventDate` when it is passed and your expected output. Will `eventDate` always be in the same format/timezone? – pilchard Sep 27 '20 at 09:53
  • what s the output you are expecting for `dateObj` – aRvi Sep 27 '20 at 10:02
  • Hi. Just realised it was wrong in the question. Its passed in as `dd/MM/YYYY` so I would expect say `02/10/2020` to come out as `Mon Oct 02 2020 00:00:00 GMT+0000 (Greenwich Mean Time)` (might not be a monday). – Chris Barrett Sep 27 '20 at 10:04
  • 1
    Does this answer your question? [How to convert dd/mm/yyyy string into JavaScript Date object?](https://stackoverflow.com/questions/33299687/how-to-convert-dd-mm-yyyy-string-into-javascript-date-object) – pilchard Sep 27 '20 at 10:05

2 Answers2

2

In the end I decided that just working with date objects from the off would be easiest

Chris Barrett
  • 571
  • 4
  • 23
0

This will give you the date object of the string in the format dd/MM/YYYY

function ddmmyyyytFormatterWithTime(date) {
  var dateDiv = date.split('/');
  var date = new Date();
  date.setDate(dateDiv[0]);
  date.setMonth(--dateDiv[1]); // -- since it starts from 0
  date.setFullYear(dateDiv[2]--);

  return date;
};

function ddmmyyyytFormatter(date) {
  var dateDiv = date.split('/');
  var date = new Date();
  date.setHours(0, 0, 0, 0); // to reset time
  date.setDate(dateDiv[0]);
  date.setMonth(--dateDiv[1]); // -- since it starts from 0
  date.setFullYear(dateDiv[2]); 

  return date;
};

var date = ddmmyyyytFormatter("10/02/2020");
var dateWithTime = ddmmyyyytFormatterWithTime("10/02/2020");
aRvi
  • 2,203
  • 1
  • 14
  • 30