# Calculate Last Date of a Month

**URL:** <https://discourse.nodered.org/t/calculate-last-date-of-a-month/56976>\
**Category:** General\
**Created:** [20 January 2022 15:24 UTC](https://discourse.nodered.org/t/calculate-last-date-of-a-month/56976 "2022-01-20T15:24:55Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [20 January 2022 15:35 UTC](https://discourse.nodered.org/t/calculate-last-date-of-a-month/56976/2 "2022-01-20T15:35:14Z")

</div>

In a function...

```auto
const selectedDate = new Date(msg.payload); //assuming date picker date is in msg.payload
const month = selectedDate.getMonth();
const year = selectedDate.getFullYear();
//msg.payload = new Date(year, month, 0).getDate(); //returns 31 for this month
//msg.payload = new Date(year, month, 0); //returns date object for last day of month
return msg;

```

Or

```auto
const selectedDate = new Date(msg.payload); //assuming date picker date is in msg.payload
const month = selectedDate.getMonth();
const year = selectedDate.getFullYear();
msg.payload = daysInMonth(month+1, year);
return msg;

/** month 1 = Jan */
function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth - 1, 32).getDate();
}

```

---

_[View the full topic](https://discourse.nodered.org/t/calculate-last-date-of-a-month/56976)._
