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

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

[{"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.

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

[{"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":[]}]

3 Likes