Exercise - Use JavaScript to find the last day of the current month
In this exercise, you'll create a flow that calculates the last day of the current month using JavaScript code.
Launch the Power Automate for desktop console and create a new flow named Last day of the month.
Under Actions, search for get current. Then select Get current date and time.
Use a Get current date and time action to store the current date in a datetime variable.
Our next actions include variables. For those, expand Variables and double-click Set variable.
Double-click the default NewVar and change it to Month.
Next, select {x} and under Flow variables, CurrentDateTime select .Month. Select Select.
Follow the same steps to add the Year variable as well.
Search for run java under Actions and double-click Run JavaScript.
In the JavaScript to run, type in var month =, then select {x}, select %Month% and then select the Select button. Then add ;.
In a new line type in var d = new Date( then select {x}, select %Year% and then select the Select button. Then add , month);.
In a new line type in WScript.Echo(d);.
Next, select the Variables produced option and toggle on ScriptError. Then select Save
Search for the Parse text action in the workspace and use the presented regular expression to remove the date's unnecessary parts.
Use a Convert text to datetime action to convert the parsed text into a datetime variable.
Reconvert the datetime value to text using the Convert datetime to text action. We're deploying this action to create a custom format of the date.
To display the final text variable that contains the last day of the month, use a Display message action.
Save the flow and then execute it. You can try different time zones to ensure that the flow can handle all possible scenarios.