AdWords
5.3K members online now
5.3K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

Script: How to Get Number of Days Remaining in Month

Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

Hi guys,

 

I'm hoping this will be an easy one for the right person. I'm trying to get the number of days that are remaining in the current month using Adwords scripts. Can someone help me with this please?

 

Thanks

 

 

1 Expert replyverified_user

Re: Script: How to Get Number of Days Remaining in Month

Top Contributor Alumni
# 2
Top Contributor Alumni

Hi Rashed,

 

Not exactly sure what you're trying to do, but the the two links below may be useful as they contain the logic to countdown or calculate days remaining.

 

Countdown Ads

 

Flexible Budgets - Manager Account

 

Re: Script: How to Get Number of Days Remaining in Month

[ Edited ]
Participant ✭ ✭ ✭
# 3
Participant ✭ ✭ ✭

Hi Rashed K,

You can get there using Date(). This is something I used in one of my budget scripts, but there is probably an easier way to do it as I am not the best coder:

var date = new Date()
var day = date.getDate()
var year = date.getFullYear()
var month = date.getMonth() +1
function numberOfDays(year, month) {
var d = new Date(year, month, 0);
return d.getDate();
}
var daysInMonth = numberOfDays(year, month)

You can then subtract the day (day number of current date) from daysInMonth (number of days in the current month) to get the number of days left in the month (so daysInMonth - day):

 

var daysLeft = daysInMonth - day

Perhaps someone will be able to jump in with a better way or a cleaner way, but if not that should work.

Goodluck!

Re: Script: How to Get Number of Days Remaining in Month

Follower ✭ ☆ ☆
# 4
Follower ✭ ☆ ☆
Hi Brian,

Thanks for sending these links but unfortuantely they don;t quite help me. I've taken a look at the Flexible budgets script hoping that I'd be able to find something in there that would show me how to do what I need to do but no joy I'm afraid.

I'm trying to put together a relatively simple script that projects the monthly spend by looking at the monthly spend so far but to put together some projections, I need to know how many days in the month have passed and how many days are remaining, Does anyone know if there's something in Adwords Scripts that does something like show the month/number of days in the month/current date etc?

Thanks
Marked as Best Answer.
Solution
Accepted by topic author Rashed K
February 2016

Re: Script: How to Get Number of Days Remaining in Month

Top Contributor
# 5
Top Contributor

Hi @Rashed K this is what you need; I use this function all the time:

 

function getDays() {
  var d = new Date(Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(), "MMM dd,yyyy HH:mm:ss"));
  var totalDays = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();
  // daysSoFar is -1 because "today" hasn't happened yet
  var daysSoFar = d.getDate() - 1;
  return {daysLeft:(totalDays - daysSoFar),totalDays:totalDays};
}

the function will return an object with two values: daysLeft and totalDays.  For example, try this in your main() function:

 

Logger.log(getDays().daysLeft + ", " + getDays().totalDays);

Jon

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Script: How to Get Number of Days Remaining in Month

Follower ✭ ☆ ☆
# 6
Follower ✭ ☆ ☆
Hi,

Sorry for the late reply. This has answered my question. Thanks a lot for your help Clynton and (as always) thanks for your help Jon. I was secretly hoping you would get involved.

Regards
RK