Exercise - Use JavaScript to find the last day of the current month

Completed

In this exercise, you'll create a flow that calculates the last day of the current month using JavaScript code.

  1. Launch the Power Automate for desktop console and create a new flow named Last day of the month.

    Screenshot of the Power Automate for desktop Build a flow dialog.

  2. Under Actions, search for get current. Then select Get current date and time.

    Screenshot of the Power Automate for desktop Get current date and time action.

  3. Use a Get current date and time action to store the current date in a datetime variable.

    Screenshot of the Power Automate for desktop Get current date and time parameters.

  4. Our next actions include variables. For those, expand Variables and double-click Set variable.

    Screenshot of the Power Automate for desktop Set variable action.

  5. Double-click the default NewVar and change it to Month.

    Screenshot of the Power Automate for desktop Set new variable action.

  6. Next, select {x} and under Flow variables, CurrentDateTime select .Month. Select Select.

    Screenshot of the Power Automate for desktop Set variable action with variable NewVar.

  7. Follow the same steps to add the Year variable as well.

    Screenshot of the Power Automate for desktop Set year variable action.

    Screenshot of the Power Automate for desktop Set variable month and year action.

  8. Search for run java under Actions and double-click Run JavaScript.

    Screenshot of the Power Automate for desktop Run JavaScript action.

  9. In the JavaScript to run, type in var month =, then select {x}, select %Month% and then select the Select button. Then add ;.

    Screenshot of the Power Automate for desktop Run JavaScript add month action.

    Screenshot of the Power Automate for desktop Run JavaScript action with parameter set to month.

  10. In a new line type in var d = new Date( then select {x}, select %Year% and then select the Select button. Then add , month);.

  11. In a new line type in WScript.Echo(d);.

    Screenshot of the Power Automate for desktop Run JavaScript action parameters.

    Next, select the Variables produced option and toggle on ScriptError. Then select Save

    Screenshot of the Power Automate for desktop Run JavaScript action parameters with ScriptError highlighted next to Variables produced.

  12. Search for the Parse text action in the workspace and use the presented regular expression to remove the date's unnecessary parts.

    Screenshot of the Power Automate for desktop Parse text action.

  13. Use a Convert text to datetime action to convert the parsed text into a datetime variable.

    Screenshot of the Power Automate for desktop Convert text to datetime action.

  14. 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.

    Screenshot of the Power Automate for desktop Convert datetime to text action.

  15. To display the final text variable that contains the last day of the month, use a Display message action.

    Screenshot of the Power Automate for desktop Display message action.

  16. Save the flow and then execute it. You can try different time zones to ensure that the flow can handle all possible scenarios.

    Screenshot of the Power Automate for desktop final flow and the save and run buttons.