# How to get Startdate and end date of a month for a given date

**URL:** <https://discourse.nodered.org/t/how-to-get-startdate-and-end-date-of-a-month-for-a-given-date/95457>\
**Category:** General\
**Tags:** function-node, jsonata\
**Created:** [17 February 2025 10:19 UTC](https://discourse.nodered.org/t/how-to-get-startdate-and-end-date-of-a-month-for-a-given-date/95457 "2025-02-17T10:19:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [17 February 2025 10:19 UTC](https://discourse.nodered.org/t/how-to-get-startdate-and-end-date-of-a-month-for-a-given-date/95457/1 "2025-02-17T10:19:14Z")

</div>

I need to get starting day of the month and ending day of the month for a given date.  
how do i write a simple function for the same. I tried from this link, but i am unable to get the start day.

`[Calculate Last Date of a Month - #2 by Steve-Mcl](https://discourse.nodered.org/t/calculate-last-date-of-a-month/56976/2)

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [17 February 2025 10:40 UTC](https://discourse.nodered.org/t/how-to-get-startdate-and-end-date-of-a-month-for-a-given-date/95457/2 "2025-02-17T10:40:59Z")

</div>

```auto
[{"id":"d66fd75b480f4413","type":"change","z":"62c89c281282ae06","name":"","rules":[{"t":"set","p":"month","pt":"msg","to":"$moment(payload).format(\"MM\")","tot":"jsonata"},{"t":"set","p":"year","pt":"msg","to":"$moment(payload).format(\"YYYY\")","tot":"jsonata"},{"t":"set","p":"date","pt":"msg","to":"$moment(payload).format(\"DD\")","tot":"jsonata"},{"t":"set","p":"firstdate","pt":"msg","to":"$moment(payload).format(\"YYYY-MM-\")&'01'","tot":"jsonata"},{"t":"set","p":"nextmonthstart","pt":"msg","to":"$moment().add(1, \"month\").format(\"YYYY-MM-\")&'01'","tot":"jsonata"},{"t":"set","p":"monthend","pt":"msg","to":"$moment(nextmonthstart).subtract(1, \"days\").format(\"YYYY-MM-DD\")","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.from","pt":"msg","to":"firstdate","tot":"msg"},{"t":"set","p":"payload.to","pt":"msg","to":"monthend","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":500,"wires":[["929faf44c44276d3","9bbfc0a7d5aa49cd"]]},{"id":"929faf44c44276d3","type":"debug","z":"62c89c281282ae06","name":"debug 2921","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":500,"wires":[]},{"id":"1a893c2dae05fbba","type":"inject","z":"62c89c281282ae06","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"object","payloadType":"date","x":160,"y":500,"wires":[["d66fd75b480f4413"]]}]

```

I think this is a long and dirty way, but it works.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 February 2025 11:07 UTC](https://discourse.nodered.org/t/how-to-get-startdate-and-end-date-of-a-month-for-a-given-date/95457/3 "2025-02-17T11:07:17Z")

</div>

Dayjs has functions to create date objects for the start and end of a period (day, month, etc) and for displaying components of the object.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dcfd2ec8b38affb719b2d9b7f67cbc278e092880.png)

```auto
[{"id":"944442a05b9dab6c","type":"inject","z":"4bf0620a5d3d07da","name":"Timestamp","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":400,"wires":[["f64821f52b853ed0"]]},{"id":"f64821f52b853ed0","type":"function","z":"4bf0620a5d3d07da","name":"First & last days","func":"const firstDay = dayjs(msg.payload).startOf(\"month\")\nconst lastDay = dayjs(msg.payload).endOf(\"month\") \nmsg.payload = { \n \"First date\": firstDay.format(\"YYYY/MM/DD\"), \n \"First day\": firstDay.format(\"dddd\"), \n \"Last date\": lastDay.format(\"YYYY/MM/DD\"), \n \"Last day\": lastDay.format(\"dddd\") \n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"dayjs","module":"dayjs"}],"x":340,"y":400,"wires":[["ea5088c27f9e4719"]]},{"id":"ea5088c27f9e4719","type":"debug","z":"4bf0620a5d3d07da","name":"Show","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":400,"wires":[]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82f574757a6bf4ede6662ed87d7542747b9cc45b.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 March 2025 11:07 UTC](https://discourse.nodered.org/t/how-to-get-startdate-and-end-date-of-a-month-for-a-given-date/95457/4 "2025-03-03T11:07:46Z")

</div>

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