# Calculate the dates correctly

**URL:** https://community.glideapps.com/t/calculate-the-dates-correctly/68736
**Category:** Ask for Help
**Created:** [December 15, 2023, 12:29pm UTC](https://community.glideapps.com/t/calculate-the-dates-correctly/68736 "2023-12-15T12:29:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Alexandra\_111](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/alexandra_111/32/62686_2.png) [@Alexandra\_111](https://community.glideapps.com/u/Alexandra_111)
#### Post date: [December 15, 2023, 12:29pm UTC](https://community.glideapps.com/t/calculate-the-dates-correctly/68736/1 "2023-12-15T12:29:45Z")

</div>

Hi everyone,

I have 2 questions:

1. I want to show the date of the next payment which is between 1st and 5th of the next month. For example, today is 15 December and the interface should display “Jan 1st - 5th”

2. I want to show the date of when yearly subscription ends. For example, the beginning of the subscription is 14.08.2023, the end of the subscription is “13.08.2024”. It should also consider the leap years

But i don’t quite understand the formulas in Glide. I know it’s a complicated question, but could anyone please help me? Thanks

---

<div class="post-metadata">

### Author: ![Dilon\_Perera](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/dilon_perera/32/85652_2.png) [@Dilon\_Perera](https://community.glideapps.com/u/Dilon_Perera)
#### Post date: [December 15, 2023, 1:50pm UTC](https://community.glideapps.com/t/calculate-the-dates-correctly/68736/2 "2023-12-15T13:50:17Z")

</div>

Hi @Alexandra_111,

Not sure if there’s a easy way to achieve this but here’s my attempt with JS!

For the 1st question ( actually not sure if I understand you correctly 🙂 )

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/c/b/cb5f08dba9eb28910ba3193aafd2803b54587d59.png)

```auto
function getNextPaymentDate(p1) {
  if (!p1) {
    return '';
  }

  const today = new Date(p1);
  const nextMonth = new Date(today.setMonth(today.getMonth() + 1));

  const nextPaymentDate = nextMonth.getDate() >= 1 && nextMonth.getDate() <= 5
    ? nextMonth
    : new Date(nextMonth.getFullYear(), nextMonth.getMonth(), 1);

  return `${nextPaymentDate.toLocaleString('en-us', { month: 'short' })} 1st - 5th`;
}

const result = getNextPaymentDate(p1);
return(result);

```

* * *

For the 2nd question

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/a/8/a82c41ddef0ea29a3440850a6676a70e7c202721.png)

```auto
function getSubscriptionEndDate(p1) {
  if (!p1) {
    return '';
  }

  const startDate = new Date(p1);
  const endYear = startDate.getFullYear() + 1;
  const endDate = new Date(endYear, startDate.getMonth(), startDate.getDate());

  if ((endYear % 4 === 0 && endYear % 100 !== 0) || (endYear % 400 === 0)) {
    endDate.setDate(endDate.getDate() - 1); 
  }

  return endDate.toLocaleDateString('en-US');
}

const result = getSubscriptionEndDate(p1);
return(result);

```

Thank you

---

<div class="post-metadata">

### Author: ![Alexandra\_111](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/alexandra_111/32/62686_2.png) [@Alexandra\_111](https://community.glideapps.com/u/Alexandra_111)
#### Post date: [December 15, 2023, 3:00pm UTC](https://community.glideapps.com/t/calculate-the-dates-correctly/68736/3 "2023-12-15T15:00:37Z")

</div>

Hi @Dilon_Perera

Looks like everything works correctly! 🤩

Thanks a lot, you made my day

---

<div class="post-metadata">

### Author: ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)
#### Post date: [December 16, 2023, 11:52pm UTC](https://community.glideapps.com/t/calculate-the-dates-correctly/68736/4 "2023-12-16T23:52:22Z")

</div>

Please test if this works correctly in iOS/MacOS. I have been bitten by using Date functions in JS with iOS/MacOS before.

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)
#### Post date: [December 23, 2023, 11:53pm UTC](https://community.glideapps.com/t/calculate-the-dates-correctly/68736/5 "2023-12-23T23:53:13Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
