Setting Time of Day and Weekday Conditions

Problem

You want users to have different experience inside and outside of working hours, additionally, we want to be able to set different working hours for each week day.

Solution

Use military time representation and simple number comparison.

Function node get current time does the time work, you can see that it has two outputs: - top output - is used when the current time is during work hours - between, work start and work end time. - bottom output - is used when the current time is outside of the working hours

Use JavaScript Date.getDay() function which returns number representing day of the week - Sunday=0, Monday=1 etc.
Followed by a Switch node that will use exit port based on the day of the week. today-switch.png

Example

time-of-the-day-and-weekday.png

[{"id":"3cb22000.41aeb","type":"function","z":"a635abea.f55998","name":"get today's day","func":"\nconst getToday = (dayNumber) => {\n    switch (dayNumber) {\n      case 0:\n        return 'Sunday';\n      case 1:\n        return 'Monday';\n      case 2:\n        return 'Tuesday';\n      case 3:\n        return 'Wednesday';\n      case 4:\n        return 'Thursday';\n      case 5:\n        return 'Friday';\n      case 6:\n        return 'Saturday';\n    }\n}\n\nconst dayNumber = (new Date()).getDay();\n\nmsg.payload.user.today = getToday(dayNumber)\nmsg.payload.user.today = getToday(6)\nreturn msg\n\n\n","outputs":1,"noerr":0,"x":620,"y":600,"wires":[["c42d895c.a262d8"]]},{"id":"c42d895c.a262d8","type":"switch","z":"a635abea.f55998","name":"Days of the week","property":"payload.user.today","propertyType":"msg","rules":[{"t":"eq","v":"Monday","vt":"str"},{"t":"eq","v":"Tuesday","vt":"str"},{"t":"eq","v":"Wednesday","vt":"str"},{"t":"eq","v":"Thursday","vt":"str"},{"t":"eq","v":"Friday","vt":"str"},{"t":"eq","v":"Saturday","vt":"str"},{"t":"eq","v":"Sunday","vt":"str"}],"checkall":"true","repair":false,"outputs":7,"x":830,"y":600,"wires":[["8464c4c9.374f98"],["8464c4c9.374f98"],["8464c4c9.374f98"],["8464c4c9.374f98"],["8464c4c9.374f98"],["2c1dd367.88e8cc"],["99af6010.7af8d"]]},{"id":"8464c4c9.374f98","type":"change","z":"a635abea.f55998","name":"Weekday Work Hours","rules":[{"p":"payload.user.workHoursStart","tot":"str","to":"9:00","t":"set","pt":"msg"},{"p":"payload.user.workHoursEnd","tot":"str","to":"16:30","t":"set","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":580,"wires":[["d41406f9.0676b8"]]},{"id":"2c1dd367.88e8cc","type":"change","z":"a635abea.f55998","name":"Saturday Work Hours","rules":[{"p":"payload.user.workHoursStart","tot":"str","to":"10:00","t":"set","pt":"msg"},{"p":"payload.user.workHoursEnd","tot":"str","to":"17:30","t":"set","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":620,"wires":[["d41406f9.0676b8"]]},{"id":"d41406f9.0676b8","type":"function","z":"a635abea.f55998","name":"get current time","func":"const date = new Date();\n\nconst currentMil = parseInt(`${date.getHours()}${date.getMinutes()}`, 10);\n\nconst startMil = parseInt(msg.payload.user.workHoursStart.split(':').join(''), 10);\nconst endMil = parseInt(msg.payload.user.workHoursEnd.split(':').join(''), 10);\n\nmsg.payload.user.dev = {currentMil, startMil,endMil };\n\nif (currentMil < startMil) {\n    // TOO EARLY\n    msg.payload.user.outsideWorkingHours = true;\n    return [null, msg];\n}\n\nif (currentMil > endMil) {\n    // TOO LATE\n    msg.payload.user.outsideWorkingHours = true;\n    return [null, msg]\n}\n\nmsg.payload.user.outsideWorkingHours = false;\nreturn [msg, null]\n\n\n","outputs":2,"noerr":0,"x":1340,"y":600,"wires":[["6c7e9da2.4ea4b4"],["99af6010.7af8d"]]},{"id":"6c7e9da2.4ea4b4","type":"dialogue","z":"a635abea.f55998","name":"Working hours","message":"Working hours","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":1540,"y":560,"wires":[[]]},{"id":"99af6010.7af8d","type":"dialogue","z":"a635abea.f55998","name":"Out of Hours","message":"Out of Hours","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":1530,"y":640,"wires":[[]]}]

To try it out simply copy the above json and import it in the flow designer.
Key shortcut Ctrl/⌘-i, Menu option Import

Discussion

Set your working hours

You will need to set your working hours, you must use 24 hour time format. set-working-hours.png