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

Completed

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

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

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

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

    Screenshot of the Power Automate desktop app 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 desktop app Get current date and time parameters.

  4. The next actions include variables. Expand Variables and double-click Set variable.

    Screenshot of the Power Automate desktop app Set variable action.

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

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

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

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

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

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

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

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

    Screenshot of the Power Automate desktop app Run JavaScript action.

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

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

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

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

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

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

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

    Screenshot of the Power Automate desktop app 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 desktop app 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 desktop app Convert text to datetime action.

  14. Use the Convert datetime to text action to convert the datetime value back to text. Deploy this action to create a custom format for the date.

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

  15. Use a Display message action to show the final text variable that contains the last day of the month.

    Screenshot of the Power Automate desktop app Display message action.

  16. Save the flow and then run it. Try different time zones to make sure the flow can handle all possible scenarios.

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