Time format conversion

This is a peace of the flow which shows my problem:

Sunrise and sunset comes in that format: "Sat Nov 25 2023 19:57:52 GMT+0100" and I need a timestamp of this format in oder to calculate the time difference. (duration of night and day for example). Therefore your last response looks like a good solution.

actually it runs with moment node but I want to get rid of it and use change nodes.

Hi @juntiedt

Whatever property that value is coming from, use it as part of a new Date constructor, and overwrite

msg.date = new Date(msg.date).getTime()

The getTime() method of Date instances returns the number of milliseconds for this date since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC.

This will you give you the timestamp, see the fiddle I have quickly wrote

1 Like

yes, that looks perfect and professional
excactly what I need.
thanks

You can do the calculation in the change node using moment().diff()
e.g.

   $moment($$.payload.sunrise).diff($moment($$.payload.sunset), "seconds")	

where msg.payload.sunset and sunrise are in format

{"sunset":"Sat Nov 25 2023 19:57:52 GMT+0100","sunrise":"Sun Nov 26 2023 07:57:52 GMT+0100"}

output in seconds

43200

Is this related to your other topic Problem with moment
If so please do not double post.

2 Likes

nice!
This makes it real easy to calculate day and night duration.

All you need with sunset and sunrise:
results are only correct if you call it after sunset and before sunrise! Here short after midnight

[{"id":"5ec2b097bdf3d8c7","type":"cronplus","z":"b0bb63dea1066d9a","name":"soll temp","outputField":"payload","timeZone":"","storeName":"","commandResponseMsgOutput":"output2","defaultLocation":"","defaultLocationType":"default","outputs":2,"options":[{"name":"schedule1","topic":"temp SK1","payloadType":"json","payload":"{\"room\":\"UGGYM\",\"type\":\"soll\",\"value\":\"16\"}","expressionType":"cron","expression":"0 */2 0-6 * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule2","topic":"temp SK2","payloadType":"json","payload":"{\"room\":\"UGGYM\",\"type\":\"soll\",\"value\":\"18\"}","expressionType":"cron","expression":"0 */2 7-8 * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule3","topic":"temp SK3","payloadType":"json","payload":"{\"room\":\"UGGYM\",\"type\":\"soll\",\"value\":\"16\"}","expressionType":"cron","expression":"0 */2 9-23 * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":420,"y":140,"wires":[["e8bdad8e628ad292"],["4fec987f.72b318"]]},{"id":"be95831c.56938","type":"inject","z":"b0bb63dea1066d9a","name":"at 00:02","props":[{"p":"payload"}],"repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":140,"wires":[["ce186cd3.00ed4"]]},{"id":"ce186cd3.00ed4","type":"function","z":"b0bb63dea1066d9a","name":"","func":"msg.payload = {\"command\":\"describe\",\n               \"expressionType\":\"solar\",\n               \"location\":\"48.38157356491496 10.437845885753632\",\n               \"solarType\":\"selected\",\n               \"solarEvents\":\"sunrise,sunset\",\n               \"timeZone\":\"Europe/Berlin\"\n              };\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":140,"wires":[["5ec2b097bdf3d8c7"]]},{"id":"e8bdad8e628ad292","type":"link out","z":"b0bb63dea1066d9a","name":"link out 126","mode":"link","links":["81caa854a65b5d8e"],"x":535,"y":120,"wires":[]},{"id":"5355c793.e8c0b8","type":"ui_template","z":"b0bb63dea1066d9a","group":"8784088a.4ec318","name":"","order":8,"width":16,"height":22,"format":"<style type=\"text/css\">\n.t1  {border-collapse:collapse;border-spacing:0;border-color:#ccc;}\n.t1 td{font-family:Arial, sans-serif;font-size:14px;padding:8px 3px;border-style:solid;border-width:0px;overflow:hidden;word-break:normal;border-top-width:1px;border-bottom-width:1px;border-color:#ccc;color:#333;background-color:#fff;}\n.t1 th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}\n.t1 .t1-36r9{background-color:#000000;color:#ffffff;text-align:left;vertical-align:top}\n.t1 .t1-x2zo{background-color:#c0c0c0;font-size:small;text-align:left;vertical-align:top}\n.t1 .t1-o73e{background-color:#c0c0c0;font-size:small;text-align:center;vertical-align:top}\n.t1 .t1-5qt9{font-size:small;text-align:left;vertical-align:top}\n.t1 .t1-5qta{font-size:small;text-align:center;vertical-align:middle}\n</style>\n<table class=\"t1\">\n  <tr>\n    <th class=\"t1-36r9\" colspan=\"9\">CRON Schedules</th>\n  </tr>\n  <tr>\n    <td class=\"t1-x2zo\">Name</td>\n    <td class=\"t1-x2zo\">Expression</td>\n    <td class=\"t1-x2zo\">Payload</td>\n    <td class=\"t1-x2zo\">Description</td>\n    <td class=\"t1-x2zo\">Next run</td>\n    <td class=\"t1-x2zo\">State</td>\n    <!-- <td class=\"t1-o73e\">Delete</td>\n    <td class=\"t1-o73e\">Pause</td>\n    <td class=\"t1-o73e\">Resume</td> -->\n  </tr>\n  <tr  ng-repeat=\"item in msg.payload\">\n    <td class=\"t1-5qt9\">{{item.config.name}}</td>\n    <td class=\"t1-5qt9\" style=\"white-space: nowrap\" nowrap>{{item.config.expression}}</td>\n    <td class=\"t1-5qt9\">{{item.config.payload}}</td>\n    <td class=\"t1-5qt9\">{{item.status.description}}</td>\n    <td class=\"t1-5qt9\">{{item.status.nextDescription}}</td>\n    <td class=\"t1-5qta\"><i class=\"fa fa-{{item.status.isRunning ? 'play' : 'pause'}}\"> </i></td>\n\n   <!-- <td class=\"t1-5qta\">\n       <md-button class=\"md-raised\"\n        ng-click=\"send([[{payload:{command:'remove', name: item.config.name}}, {payload:{command:'list-all'}}]]);\"> <i class=\"fa fa-trash\"> </i></md-button>\n   </td>\n   <td class=\"t1-5qta\">\n       <md-button class=\"button\" \n       ng-click=\"send([[{payload:{command:'pause', name: item.config.name}}, {payload:{command:'list-all'}}]]);\"> <i class=\"fa fa-pause\"> </i></md-button>\n   </td>\n   <td class=\"t1-5qta\">\n       <md-button class=\"button\" \n       ng-click=\"send([[{payload:{command:'start', name: item.config.name}}, {payload:{command:'list-all'}}]]);\"> <i class=\"fa fa-play\"> </i></md-button>\n   </td> -->\n\n  </tr>\n\n </tbody>\n</table>\n\n<!--<table id=\"table\" class=\"table table-striped table-responsive-md btn-table\" >-->\n<!--     <tr>-->\n<!--        <th>Name</th> -->\n<!--        <th>Expression</th> -->\n<!--        <th>Payload</th> -->\n<!--        <th>Description</th>-->\n<!--        <th>Next run</th>-->\n<!--        <th>State</th>-->\n<!--        <th>Delete</th>-->\n<!--        <th>Pause</th>-->\n<!--        <th>Resume</th>-->\n<!-- </tr>-->\n<!-- <tbody>-->\n<!-- <tr ng-repeat=\"item in msg.payload\">-->\n<!--   <td >{{item.config.name}}</td>-->\n<!--   <td style=\"white-space: nowrap\" nowrap>{{item.config.expression}}</td>-->\n<!--   <td >{{item.config.payload}}</td>-->\n<!--   <td >{{item.status.description}}</td>-->\n<!--   <td >{{item.status.nextDescription}}</td>-->\n<!--   <td >{{item.status.isRunning ? 'Running' : 'Paused'}}</td>-->\n<!--   <td>-->\n<!--       <md-button class=\"md-raised\"-->\n<!--        ng-click=\"send([[{payload:{command:'remove', name: item.config.name}}, {payload:{command:'list-all'}}]]);\"> <i class=\"fa fa-trash\"> </i></md-button>-->\n<!--   </td>-->\n<!--   <td>-->\n<!--       <md-button class=\"button\" -->\n<!--       ng-click=\"send([[{payload:{command:'pause', name: item.config.name}}, {payload:{command:'list-all'}}]]);\"> <i class=\"fa fa-pause\"> </i></md-button>-->\n<!--   </td>-->\n<!--   <td>-->\n<!--       <md-button class=\"button\" -->\n<!--       ng-click=\"send([[{payload:{command:'start', name: item.config.name}}, {payload:{command:'list-all'}}]]);\"> <i class=\"fa fa-play\"> </i></md-button>-->\n<!--   </td>-->\n<!-- </tr>-->\n<!-- </tbody>-->\n<!--</table>-->\n\n","storeOutMessages":false,"fwdInMessages":false,"resendOnRefresh":false,"templateScope":"local","x":840,"y":240,"wires":[[]]},{"id":"3b9c3e39.b0eaf2","type":"function","z":"b0bb63dea1066d9a","name":"status-all","func":"if(!msg.payload || !msg.payload.result || !msg.payload.result.length){\n    msg.payload = [];\n    return msg;\n}\nmsg.payload = msg.payload.result;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":840,"y":180,"wires":[["5355c793.e8c0b8"]]},{"id":"4fec987f.72b318","type":"switch","z":"b0bb63dea1066d9a","name":"","property":"payload.command.command","propertyType":"msg","rules":[{"t":"eq","v":"describe","vt":"str"},{"t":"eq","v":"status-all","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":140,"wires":[["8cf018fb.860648"],["3b9c3e39.b0eaf2"]]},{"id":"8cf018fb.860648","type":"function","z":"b0bb63dea1066d9a","name":"get sr + ss items","func":"\nvar sunrise = msg.payload.result.eventTimes.find(e => {\n    return e.event == \"sunrise\"\n})\n\nvar sunset = msg.payload.result.eventTimes.find(e => {\n    return e.event == \"sunset\"\n})\n\nmsg.payload = {\n    sunrise : sunrise.time,\n    sunset : sunset.time\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":860,"y":100,"wires":[["ee44abb6ddb654f2","d7dc178406cca37f","24c7204af80a292c","e9b811fc3785e054","f5b6ab6f20dc57ce","69fc9cec8a1a30d2","d979e893426fdca7"]]},{"id":"d7dc178406cca37f","type":"change","z":"b0bb63dea1066d9a","name":"sunset DD-MM-YYYY HH:mm:ss","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload.sunset).format(\"DD-MM-YYYY HH:mm:ss\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1180,"y":60,"wires":[["f8c3ddbf3a91d37a"]]},{"id":"f8c3ddbf3a91d37a","type":"debug","z":"b0bb63dea1066d9a","name":"debug 160","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1530,"y":60,"wires":[]},{"id":"ee44abb6ddb654f2","type":"change","z":"b0bb63dea1066d9a","name":"sunrise DD-MM-YYYY HH:mm:ss","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload.sunrise).format(\"DD-MM-YYYY HH:mm:ss\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1180,"y":20,"wires":[["9ebc2fdfb4f8025b"]]},{"id":"9ebc2fdfb4f8025b","type":"debug","z":"b0bb63dea1066d9a","name":"debug 162","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1530,"y":20,"wires":[]},{"id":"f5b6ab6f20dc57ce","type":"change","z":"b0bb63dea1066d9a","name":"sunrise-sunset difference","rules":[{"t":"set","p":"payload","pt":"msg","to":"  $moment($$.payload.sunset).diff($moment($$.payload.sunrise), \"seconds\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1150,"y":260,"wires":[["4cdee8ee56e9a9c0","c3ec85c524df8a5d"]]},{"id":"c3ec85c524df8a5d","type":"function","z":"b0bb63dea1066d9a","name":"day & night duration","func":"let day = 60*60*24;\nlet tag = msg.payload;\nlet nacht = day - tag;\nlet nacht_total = duration_str(nacht);\nlet tag_total = duration_str(tag);\nmsg.payload = \"Tag: \" + tag_total + \" / \" + \"Nacht: \" + nacht_total;\nreturn msg;\n\nfunction duration_str(sec) {\n    let t = sec;\n    let h = (Math.floor(t / 3600)).toString();\n    let m = (Math.floor(t % 3600 / 60)).toString();\n//    let s = (Math.floor(t % 3600 % 60)).toString();\n//    let r = h + \":\" + (\"0\" + m).slice(-2) + \":\" + (\"0\" + s).slice(-2);\n    let r = h + \":\" + (\"0\" + m).slice(-2);\n    return r;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":300,"wires":[["f1df25ceb28d4b30"]]},{"id":"f1df25ceb28d4b30","type":"debug","z":"b0bb63dea1066d9a","name":"debug 2470","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1530,"y":300,"wires":[]},{"id":"4cdee8ee56e9a9c0","type":"debug","z":"b0bb63dea1066d9a","name":"debug 2474","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1530,"y":260,"wires":[]},{"id":"e9b811fc3785e054","type":"change","z":"b0bb63dea1066d9a","name":"sunset HH:mm","rules":[{"t":"set","p":"payload","pt":"msg","to":"  $moment(payload.sunset).format(\"HH:mm\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":140,"wires":[["c1fb072b6dea4d89"]]},{"id":"c1fb072b6dea4d89","type":"debug","z":"b0bb63dea1066d9a","name":"debug 2475","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1530,"y":140,"wires":[]},{"id":"24c7204af80a292c","type":"change","z":"b0bb63dea1066d9a","name":"sunrise HH:mm","rules":[{"t":"set","p":"payload","pt":"msg","to":"  $moment(payload.sunrise).format(\"HH:mm\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":100,"wires":[["51ce1d0e365b47b0"]]},{"id":"51ce1d0e365b47b0","type":"debug","z":"b0bb63dea1066d9a","name":"debug 2476","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1530,"y":100,"wires":[]},{"id":"e39fbd0b68945209","type":"debug","z":"b0bb63dea1066d9a","name":"debug 2480","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1530,"y":180,"wires":[]},{"id":"89423b49b2a93387","type":"debug","z":"b0bb63dea1066d9a","name":"debug 2481","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1530,"y":220,"wires":[]},{"id":"69fc9cec8a1a30d2","type":"function","z":"b0bb63dea1066d9a","name":"sunrise timestamp ","func":"return {payload : new Date(msg.payload.sunrise).getTime()};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1130,"y":180,"wires":[["e39fbd0b68945209"]]},{"id":"d979e893426fdca7","type":"function","z":"b0bb63dea1066d9a","name":"sunset timestamp ","func":"return {payload : new Date(msg.payload.sunset).getTime()};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1130,"y":220,"wires":[["89423b49b2a93387"]]},{"id":"8784088a.4ec318","type":"ui_group","name":"CRON","tab":"9920c734.9de2a8","order":3,"disp":false,"width":"16","collapse":false},{"id":"9920c734.9de2a8","type":"ui_tab","name":"CRON Plus","icon":"dashboard","order":22,"disabled":false,"hidden":false}]

Then you need to determine when the expression is called and alter your moment().diff() calculation accordingly.

yes, I know. In my case I have more jobs running at midnight and therefore I just added sunset sunrise routines to it.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.