date range from first day of the month to yesterday

876 views
Skip to first unread message

aless...@valnan.it

unread,
Jul 21, 2017, 9:20:42 AM7/21/17
to AdWords Scripts Forum
Hello,

Scripts newbie here.

I'd like to have a report ranging from the first day of the current month to the current day - 1 (in other words, the previous day of any given day the scripts run. aka yesterday...)

At the moment I'm using the function below which let me use the var "yesterday" with .getStatsFor.

I would like to use a variable also for the first day of the current month instead of manually inserting it like I'm doing right now:

.getStatsFor("20170701", yesterday);

Is it possible amending the function below?


function format_date(date){
  var date = date;
  var yyyy = date.getFullYear().toString();
  var mm = (date.getMonth()+1).toString();
  var dd  = date.getDate().toString();
  var mmChars = mm.split('');
  var ddChars = dd.split('');
  var datestring = yyyy + (mmChars[1]?mm:"0"+mmChars[0]) + (ddChars[1]?dd:"0"+ddChars[0]);
  return datestring;
} 
  
  var date = new Date();
  date.setDate(date.getDate() - 1);
  yesterday = format_date(date);
  Logger.log(yesterday);

Thanks!
Alex

Anthony Madrigal

unread,
Jul 21, 2017, 11:48:13 AM7/21/17
to AdWords Scripts Forum
Hi Alex,

You can add the following function which will return both the beginning of the month date and yesterday as a string which you can use for your date range.
function getDates(){
 
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
 
var now = new Date();
 
var firstDay = new Date(now.getFullYear(), now.getMonth(), 1); // return first day of the month
 
var yesterday = new Date(now.getTime() - MILLIS_PER_DAY);
 
var timeZone = AdWordsApp.currentAccount().getTimeZone();
 
 
return Utilities.formatDate(firstDay, timeZone, 'yyyyMMdd') + ', ' + Utilities.formatDate(yesterday, timeZone, 'yyyyMMdd');
}

Cheers,
Anthony
AdWords Scripts Team

aless...@valnan.it

unread,
Jul 21, 2017, 12:54:56 PM7/21/17
to AdWords Scripts Forum
Hi Anthony,

thanks a lot. That looks much simpler as well. My doubt is now what should i put in

.getStatsFor();

as if use 
getStatsFor(firstDay, yesterday);

the var firstDay is not defined.

Thanks!
A.

Anthony Madrigal

unread,
Jul 21, 2017, 2:03:14 PM7/21/17
to AdWords Scripts Forum
Hi,

You could place the code as shown here to get both the start and end date:
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
var now = new Date();
var firstDay = new Date(now.getFullYear(), now.getMonth(), 1); // return first day of the month
var yesterday = new Date(now.getTime() - MILLIS_PER_DAY);
var timeZone = AdWordsApp.currentAccount().getTimeZone();
var start = Utilities.formatDate(firstDay, timeZone, 'yyyyMMdd');
var end = Utilities.formatDate(yesterday, timeZone, 'yyyyMMdd');
var kws = AdWordsApp.keywords().withCondition("Impressions > 1").forDateRange(start,end).get();

Cheers,
Anthony
AdWords Scripts Team

aless...@valnan.it

unread,
Jul 24, 2017, 5:52:22 AM7/24/17
to AdWords Scripts Forum
Hi,

Thanks a lot , worked perfectly.

Just for information sake, to use the date range in a different function I adapted the code as follow:

function getDates(){
 
var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
 
var now = new Date();
 
var firstDay = new Date(now.getFullYear(), now.getMonth(), 1); // return first day of the month
 
var yesterday = new Date(now.getTime() - MILLIS_PER_DAY);
 
var timeZone = AdWordsApp.currentAccount().getTimeZone();

  start
= Utilities.formatDate(firstDay, timeZone, 'yyyyMMdd');

 
end = Utilities.formatDate(yesterday, timeZone, 'yyyyMMdd');

 
return Utilities.formatDate(firstDay, timeZone, 'yyyyMMdd') + ', ' + Utilities.formatDate(yesterday, timeZone, 'yyyyMMdd');

}

getDates
();


Best,
Alex
Reply all
Reply to author
Forward
0 new messages