Re: [Apps-Script] How can I use the current date to create a "7 days ago" variable?

1,006 views
Skip to first unread message
Message has been deleted

Jorge Forero

unread,
Jun 18, 2020, 12:58:33 PM6/18/20
to google-apps-sc...@googlegroups.com

Vincent,

You could try this function, that I developed:

/**
* getDateNDaysBefore
* Get the date of nDays before endDate
*
* @param {date object} endDate - end date of calculation
* @param {number} nDays - Days numbers to subtract to endDate
* @return {date object} Date of nDays before endDate
**/
function getDateNDaysBefore(endDate, nDays) {
  var daysBefore = endDate - 1000 * 60 * 60 * 24 * nDays;
  var StartDate = new Date(daysBefore);
  return StartDate;
};

Jorge.



On Thu, Jun 18, 2020 at 10:38 AM Vincent Adipietro <vincent....@gmail.com> wrote:

Hi everyone,

I used var currentDate = new Date(); to get the "current date". I now want to use the current date var to create a "7 days ago" variable.

For example, the current date is 6/18/2020. 7 days ago would be 6/11/2020. I want the variable to do this automatically.

I tried looking this up and there wasn't a clear answer as to how to do this. Does anyone know?

My date is formatted as "MM/dd/yyyy".

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ff4c0dbb-4a98-468b-89db-30d3d9f40f28o%40googlegroups.com.
Message has been deleted
Message has been deleted

Jorge Forero

unread,
Jun 18, 2020, 1:39:03 PM6/18/20
to google-apps-sc...@googlegroups.com
Vincent,

That is one form to do it... And I think it works...
I prefer the following:

dateToSend = getDateNDaysBefore(new Date(), 7);

and now you can send the dateToSend variable.

Greetings, Jorge.



On Thu, Jun 18, 2020 at 12:18 PM Vincent Adipietro <vincent....@gmail.com> wrote:
Hi Jorge,

Would it be:

var currentDate = new Date();
var nDays = 7;
var daysBefore = currentDate - 1000 * 60 * 60 * 24 * nDays;
var StartDate = new Date(daysBefore);
Logger.log(StartDate);

And now I can pass StartDate into an API call by using that variable, which is the 7 days ago variable?
Message has been deleted
Message has been deleted

Jorge Forero

unread,
Jun 18, 2020, 2:04:58 PM6/18/20
to google-apps-sc...@googlegroups.com
Vincent,

>> Do you know how I can make the function you gave me universal? Bc I will be using the dateToSend variable in many different functions.

When you say: "universal",  what does that mean?    You could include the function as part of a Library, include the library in your code and call the function where you need it.  Here is the documentation about libraries: https://developers.google.com/apps-script/guides/libraries

or include the function as part of the script, get the value in the variable as global or pass it as a parameter in the different functions you have...

Jorge.



On Thu, Jun 18, 2020 at 12:49 PM Vincent Adipietro <vincent....@gmail.com> wrote:
Oh I see - disregard my last message. I have to call the function to be able to use this part. And it'll make it quicker I bet.

Do you know how I can make the function you gave me universal? Bc I will be using the dateToSend variable in many different functions.
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages