# Find the last day of the following month

**URL:** https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557
**Category:** Ask for Help
**Tags:** holdmybeer
**Created:** [January 22, 2021, 6:02am UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557 "2021-01-22T06:02:57Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Roldy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/roldy/32/36068_2.png) [@Roldy](https://community.glideapps.com/u/Roldy)
#### Post date: [January 22, 2021, 6:02am UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/1 "2021-01-22T06:02:57Z")

</div>

I was wondering if it is possible, directly in Glide (without using sheet), to calculate the last calendar day of the month following the current one.  
For example: today is January 22, the date I need is February 28, 2021.

This is useful for some payment installments in commercial contracts.  
Thanks

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 7:19am UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/2 "2021-01-22T07:19:26Z")

</div>

ooooh, I can’t wait to see what @Jeff_Hager comes up with for this one 😆 🤣

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 10:28am UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/3 "2021-01-22T10:28:46Z")

</div>

I’ve figured out a brute force approach that I believe will work. I’ll post it once I’ve had a chance to test and validate it.

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 12:57pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/4 "2021-01-22T12:57:17Z")

</div>

Okay, I have a “solution”. And it _ONLY_ requires 40 math columns and 3 if-then-else columns 🤣 🤣

So no, please don’t take this as a serious suggestion. I just enjoy a challenge 😆

> **My approach...**
>
> - The first thing that occurred to me when I started thinking about this is that if we know (or can discover) how many days there are between “today” and the last day of next month, then the answer is simple. It’s just `NOW + n`, where `n` is the number of days.
> - So how to figure out what `n` is, when you don’t know in advance what `NOW` is?
> - One thing I realised pretty quickly is that `n` is a finite set of sequential integers, where:
> - the lower bounds is the number of days between Jan 31 and Feb 28 (28, in a non-leap year)
> - the upper bounds is the number of days between Jul 1 and Aug 31 (61)
> 
> - Okay. So that means that `n` will _always_ be an integer between 28 and 61 for any given date.
> 
> And this is what led me to realise that it should be possible to find the answer using a brute force approach. All we need to do is figure out a way to test for all possible values of `n`, and we should eventually arrive at the correct answer.
> 
> However, there is a slight snag. And that comes with the fact that the count of all possible values of `n` (34) is greater than the minimum number of days in a month (28). And what this means is that for _some_ dates, there could be two possible answers. For example:
> 
> - Apr 1 + 28 = Apr 29 (_wrong_)
> - Apr 1 + 60 = May 31 (_correct_)
> 
> Both 28 and 60 are in the set of possible `n` values, so how to decide which is correct?

> **Step by step...**
>
> - I started off by creating 34 Math columns, labelled T28 to T62. (actually I didn’t really - I just created T28 to T35, and T55 to T62 - but that was enough to prove that it works)
> 
> - Each of those math columns uses the formula `Day(T+CN)`, where `T` is Today, and `CN` is the column number. So column T28 has `Day(T+28)`, column 29 has `Day(T+29)`, etc, etc… all the way up to column T62, which is `Day(T+62)`
> 
> - What that gives us is the day of the month of today plus the number of days represented by that column number
> 
> - Here is what that looks like in the Data Editor:  
> 
> - So now we essentially have all possible values of `n` for each of the values of Today in that table.
> 
> The next step is to figure out which is the correct one. What we are looking for is:
> 
> - The highest number in each row, BUT
> - It must be in the next month
> 
> Okay, so the next thing I did was to create two if-then-else columns, DD1 & DD2 (DD for Days Difference):
> 
> - The first one (DD1), starts at the bottom and works its way up, comparing the value of each column to the value of the preceding column. If it’s decreased, that means we’ve hit the end of the month, and so we use the number of the previous column. Here is what it looks like:
> 
> ![Screen Shot 2021-01-22 at 8.34.26 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/2/6/26a8fa1df9c979eb2cd960d1fdf60bd4b077f98e.png)
> 
> - This gives us our first “candidate” value for `n`. We don’t know if it’s correct yet because it _might_ not be in the next month.
> 
> - To get the second candidate, DD2 is used. It does the same thing as DD1, except in reverse - working from the top down.
> 
> - With 2 candidate values for `n`, we can now calculate two potential answers with two simple math columns - `Today + DD1` and `Today + DD2`, which become CD1 and CD2 respectively
> 
> - The final piece in the puzzle is to figure out which candidate is correct. For this I used the math `Month()` function:

Voila!

![Screen Shot 2021-01-22 at 8.48.53 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/9/2/925f7cbfa92742b1c3a7d19fa0a0e9fd2377dde6.png)

---

<div class="post-metadata">

### Author: ![Lucas\_Pires](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lucas_pires/32/64863_2.png) [@Lucas\_Pires](https://community.glideapps.com/u/Lucas_Pires)
#### Post date: [January 22, 2021, 1:15pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/5 "2021-01-22T13:15:23Z")

</div>

_ONLY_  
🤣

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 1:16pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/6 "2021-01-22T13:16:42Z")

</div>

hahah yeah, and Jeff will probably do it with 3 columns 🤣🤣🤣

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [January 22, 2021, 2:06pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/7 "2021-01-22T14:06:41Z")

</div>

@Darren_Murphy how about 1 column? Hold my 🍺

```auto
((Now-DAY(Now)+15)+60)
-
DAY((Now-DAY(Now)+15)+60)

```

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/5/6/5687c42677ff420cc5e5a6bc881ce845c1494ed4.png)

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 2:10pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/8 "2021-01-22T14:10:40Z")

</div>

> [@Jeff\_Hager](#):
>
> ```auto
> ((Now-DAY(Now)+15)+60)
> -
> DAY((Now-DAY(Now)+15)+60)
> 
> ```

oh dear…

🤣 🤣 🤣 🤣

---

<div class="post-metadata">

### Author: ![ehdubya](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/ehdubya/32/24087_2.png) [@ehdubya](https://community.glideapps.com/u/ehdubya)
#### Post date: [January 22, 2021, 2:11pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/9 "2021-01-22T14:11:54Z")

</div>

[![](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/4/7/47234cf3ddcc2e55d37f0113fc0caeeb37c2141d.gif) ](https://media2.giphy.com/media/l2Sq29cFXoF80ADlK/giphy.gif)

---

<div class="post-metadata">

### Author: ![Lucas\_Pires](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lucas_pires/32/64863_2.png) [@Lucas\_Pires](https://community.glideapps.com/u/Lucas_Pires)
#### Post date: [January 22, 2021, 2:25pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/10 "2021-01-22T14:25:26Z")

</div>

It would be incredible to comment if I hadn’t run out of words 😲

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 2:29pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/11 "2021-01-22T14:29:32Z")

</div>

Okay, so I’ve been playing around with that, and I can’t for the life of me figure out what’s special about the numbers 15 and 60??

---

<div class="post-metadata">

### Author: ![Robert\_Petitto](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/robert_petitto/32/25193_2.png) [@Robert\_Petitto](https://community.glideapps.com/u/Robert_Petitto)
#### Post date: [January 22, 2021, 2:39pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/12 "2021-01-22T14:39:13Z")

</div>

@Jeff_Hager  
 ![](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/2/c/2c65f5c1bd3ab2548604610bc33ca6601f13a65b.gif)

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [January 22, 2021, 2:43pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/13 "2021-01-22T14:43:04Z")

</div>

> [@Jeff\_Hager](#):
>
> ```auto
> ((Now-DAY(Now)+15)+60)
> -
> DAY((Now-DAY(Now)+15)+60)
> 
> ```

So, I’ll explain the logic a little bit. My main goal was to try to get the 1st day of the month, but two months from now. So for example, today is January 22nd. My goal was to figure out March 1st, and then simply subtract 1 day to get the last day of the previous month.

If you look at the formula, I am first taking today’s date, and subtracting the number of days from that same date. Technically that gives me December 31st as a result, but then I add 15 days. The reason I add 15 days is to get a day approximately in the middle of the current month. Now, no matter what today’s day is, we will always have a date around the 14th or 15th of the current month. Now we can add 60 days to get a date 2 months from now.

- Using the +15 is a way to guarantee that we will always end up within the middle of a month when adding 60 days. Otherwise we have those edge cases where adding 60 days to the first or last day of a month may actually put you 3 months ahead. Example: adding 60 days to December 31st or January 31st would put you three months ahead because of February being a short month. If we can guarantee that the result is in the middle of the month, then we don’t have to worry about these edge cases.

Now that we have a date that’s in the middle of a month that’s 2 months from now, we take that same formula and do it again, but this time we wrap it in the DAYS function. With that we can take the current day from the date that’s 2 months from now and subtract that from itself, which will give us last day in the month prior to that.

- So what we did is take January 22nd for example, then subtract 22 days to get December 31st.
- Next we add 15 days to get January 15th as a result
- Next we add 60 days to get March 16th as a result (technically could have just used 75 instead of 15 and 60, but 15 and 60 makes more sense visually)
- Next we do that same formula, but wrap it in the DAY function so we get the value of 16 from the March 16th date.
- Now we take March 16th minus 16 and that gives us February 28th as the final result.

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 2:47pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/14 "2021-01-22T14:47:58Z")

</div>

I take my hat off to you, sir.  
That’s such a great example of creative thinking.  
Simply Superb

---

<div class="post-metadata">

### Author: ![Lucas\_Pires](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lucas_pires/32/64863_2.png) [@Lucas\_Pires](https://community.glideapps.com/u/Lucas_Pires)
#### Post date: [January 22, 2021, 2:51pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/15 "2021-01-22T14:51:26Z")

</div>

Simply superb

![](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/7/4/7438b5426c821069190a78cd6c7b9f0eb564a4c8.gif)

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [January 22, 2021, 3:01pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/16 "2021-01-22T15:01:53Z")

</div>

Thank you sir! That’s what I like to call, “thinking backwards”. Instead of thinking through steps to get to a goal, I like to figure out the last step first. In this case, I knew I could always subtract 1 from the first day of the month and always end up with the last day of the previous month (every month has the same first day, but never has the same last day). Then my next step was to figure out, how do I get the 1st of the month. I knew I could determine the day number in a given date and subtract that number from the same date. With that it was easy to determine the first day of a month from any given date. From there is was just working out the edge cases, and that’s where I came up with the +15.

---

<div class="post-metadata">

### Author: ![Wiz.Wazeer](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/wiz.wazeer/32/72423_2.png) [@Wiz.Wazeer](https://community.glideapps.com/u/Wiz.Wazeer)
#### Post date: [January 22, 2021, 3:02pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/17 "2021-01-22T15:02:17Z")

</div>

Remarkable!

---

<div class="post-metadata">

### Author: ![Roldy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/roldy/32/36068_2.png) [@Roldy](https://community.glideapps.com/u/Roldy)
#### Post date: [January 22, 2021, 3:07pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/18 "2021-01-22T15:07:32Z")

</div>

@Jeff_Hager you are a monster!  
I thank you all for participating in the solution of this question. Thank you for taking all this time, I have to offer you all a beer!

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [January 22, 2021, 3:09pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/19 "2021-01-22T15:09:00Z")

</div>

Yes, I’ve seen you mention that thinking backwards approach before, and I actually had that in mind with my crack at it. In my case, my goal was ‘n’, because I knew once I had that I had the answer. So it was all about getting to ‘n’

Anyway, it was a fun and challenging exercise, and I bow to you once again 🙇‍♂️

PS. You should seriously consider taking up [Perl Golf](http://wiki.c2.com/?PerlGolf) 😉

---

<div class="post-metadata">

### Author: ![Robert\_Petitto](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/robert_petitto/32/25193_2.png) [@Robert\_Petitto](https://community.glideapps.com/u/Robert_Petitto)
#### Post date: [January 22, 2021, 3:21pm UTC](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557/20 "2021-01-22T15:21:05Z")

</div>

> [@Jeff\_Hager](#):
>
> I knew I could always subtract 1 from the first day of the month and always end up with the last day

This was the key to unlocking this wizardry. Simply brilliant. …wondering if I can use something like this in another app I’m making for a client that wants recurring weekly and monthly tasks. I was struggling with the monthly tasks because each month has a different number of days and I needed to show “days until”.

[Next page](https://community.glideapps.com/t/find-the-last-day-of-the-following-month/21557.md?page=2)
