Date Range Selector -> 60 days instead of 30

159 views
Skip to first unread message

Sander van Toppy

unread,
Sep 13, 2022, 4:17:36 AM9/13/22
to Google Ads Scripts Forum
Hey,

I'm looking for a way to select the last 60 days instead of LAST_30_DAYS.
I know its possible to do with a custom date range selector like:  dateRange = "20220101, 20220230" but the next day I would have to manually change te value's again.
I'm looking to select 60 days without having to manually change it every day.
Annyone have anny idea's how this can be done?

Thank you in advance,
Sander Reiniers

Sigurd Fabrin

unread,
Sep 13, 2022, 7:10:38 AM9/13/22
to Google Ads Scripts Forum
Hi Sander,

You can use Utilities.formatDate() for date formats in scripts https://developers.google.com/google-ads/scripts/docs/features/dates

Here's an example
function main() {
  let dates = getDates(180); // start date is this number of days ago
  console.log('start date: '+dates.startDate+', end date: '+dates.endDate)
 
function getDates(days) {
  let timeZone = AdsApp.currentAccount().getTimeZone();
  let format = 'yyyy-MM-dd';
  let today = new Date();
  let oneDay = 1000*60*60*24; // milliseconds
  let yesterday = Utilities.formatDate(new Date(today-oneDay),timeZone,format);
  let xDaysAgo = Utilities.formatDate(new Date(today-(oneDay*days)),timeZone,format);  
  return {startDate:xDaysAgo,endDate:yesterday}
}
}


Sigurd

Google Ads Scripts Forum Advisor

unread,
Sep 13, 2022, 1:12:30 PM9/13/22
to adwords...@googlegroups.com
Hello,

Thanks for reaching out. You can use date math to write a function that computes the 8 digit integer representation for the date, 60 days ago, and use that for the custom date range. You can use this example that retrieves a report for data from 3 days ago. You can simply substitute the third line with:

  const from = new Date(now.getTime() - 60 * MILLIS_PER_DAY);

Regards,

Google Logo
Matt
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2eNkYU:ref

Sander van Toppy

unread,
Sep 14, 2022, 5:36:45 AM9/14/22
to Google Ads Scripts Forum
Hey,

I've done the improvements to the script that u have described and now im getting the right value's, thank you for the help!

Regards,




Op dinsdag 13 september 2022 om 19:12:30 UTC+2 schreef adsscripts:
Reply all
Reply to author
Forward
0 new messages