Best answer

Calculate number of days remaining in a month

  • 14 July 2020
  • 5 replies
  • 324 views

Userlevel 1

Hello there!

I am trying to calculate the number of days remaining in a month from the Output Date.

So, for example, the output date is 18/08/2020 - what formula could I use to calculate the number of remaining days from the 18/08/2020 to the end of the month, 31/08/2020.

The output date can be any date of the year so the calculation would need to check what month it is in and how many days in that month.

Any help would be appreciated.

Thanks

icon

Best answer by ForYourIT 21 July 2020, 11:55

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

5 replies

Userlevel 7
Badge +11

Hi @Dainesj!

The cleanest way to accomplish this would be with a Code step. Unfortunately, I’m not super well-versed in Python or Javascript (though others in the Zapier Community are).

I think I could solve this with a couple of Formatter steps, and possibly Google Sheets. My day is wrapping up here, so let me jump back into this tomorrow to see if I can come up with something not too complicated.

Userlevel 7
Badge +7

Hi @Dainesj

That is a great question and very useful. Probably I will make a post about such calculations soon. But lets try to help you out on this one now.

Please keep in mind, I am no programmer myself….I got some employees that do that for me. However, I have a bit of experience and do like to create some simple scripts. 
I played around a bit, and this is what I came up with. You can use this in a code step like @nicksimard explained:

 

var formatDate = inputData.calcDate.split("/").reverse().join("-");
var d = new Date(formatDate);
var lastDay = new Date(d.getFullYear(), d.getMonth()+1, 0);

var datestring = lastDay.getDate() + "/" + ("0" + (lastDay.getMonth()+1)).slice(-2) + "/" + lastDay.getFullYear(); //Remove this if you like, or use it in the output

const oneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds
const diffDays = Math.round(Math.abs((lastDay - d) / oneDay));

output = [{daysRemaining: diffDays}];

This requires you to have an input field with the name calcDate which basically gives the date it should use to make its calculations. It would look like this:

This will output a field called daysRemaining and this can be used in later steps again. Within this script you can also output the last day of the date you gave it if you like. For this, add the output variable datestring within the output variable. If now, remove it like stated in the code.

Hope this helps you out. Like I said, I am no programmer so all created based on my own basic knowledge haha. Let me know if you need anything else!

 

~Bjorn

Userlevel 1

Thank you @ForYourIT and @nicksimard.  I will have a go with this today and let you know if I get it to work.  Very much appreciated.  Regs, Jon

Userlevel 7
Badge +11

Well, there you have it! Thanks for the amazing assist @ForYourIT :)

Userlevel 7
Badge +7

Hi @Dainesj ,

Where you able to complete this or do you still need some additional assistance? If all good, please mark your answer if you like to close this question :D

 

~ Bjorn