0
votes

In my Apps Script, I need to use the same format of (yesterday's) date in multiple different functions. So I decided to make my own custom makedate() function to reliably create this date for me when I needed it. The issue, however, is that it's returning undefined instead of my date. Here is my current function:

function makeDate(){
  var myDate = new Date();
  var date = (myDate.getDate()-1) + "-" + (myDate.getMonth() + 1) + "-" + myDate.getFullYear();
  Logger.log(date);
  return date
}

In the log file, I see that the date is being created in the format I want. However, when I then make another function to test the return value, the log file shows Made date: with no date following it, and other uses show that the value is undefined. What am I doing wrong?

2

2 Answers

0
votes

your issue is that custom functions return value must not depend on anything other than input parameters. in your case you have none so it will cache the first result and use it for further calls with the same parameters (none).

to fix you must pass the date as a parameter.

0
votes

I figured out the problem somehow had to do with variable scopes I think actually. I was using this to test code for another script within the project, not realizing that the functions within the scripts are all connected. Once I made sure there was only one instance of every function/global, the problems resolved. Dumb mistake, I know, but it's good to know for any beginners learning Google Apps Scripts so I figured it was worth posting as an answer.