# Update RPi3 DateTime by WiFi

**URL:** <https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955>\
**Category:** General\
**Created:** [11 January 2021 13:54 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955 "2021-01-11T13:54:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent78](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@Vincent78](https://discourse.nodered.org/u/Vincent78)\
**Post date:** [11 January 2021 13:54 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/1 "2021-01-11T13:54:53Z")

</div>

Hi,  
In my RPI3 projet, I use this [node](https://flows.nodered.org/flow/7e4bb517ce3a75add0083642ef03e9d8) to display DateTime in ui toolbar. When I use RPi3 WiFi remote access, ui DateTime is the PC/Smartphone DateTime. Normal.  
Now I would like update RPi3 DateTime with PC/Smartphone DateTime. Is it possible?  
I can't use RTC or Ethernet in this project.  
Thank you for yours answers.  
Vincent  
NodeRed beginner

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [11 January 2021 14:55 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/2 "2021-01-11T14:55:14Z")

</div>

Hi Vincent,

And the Wifi connection is not on the internet ?  
If not check this solution that syncs the time of a Pi from a users Browser.

> [@Clock from Phone](https://discourse.nodered.org/t/clock-from-phone/31812/12):
>
> Hi Bruno, I was working on a ui\_template example along Colin's suggestions. That is getting the time from the user browser sending to node-red and after that you have to process it with the exec node you mentioned Example flow ... [{"id":"ce3be529.f3d278","type":"ui\_template","z":"f54b08eb.e98bb","group":"c205e26f.6865a","name":"","order":0,"width":"0","height":"0","format":" \<html\>\n \<head\>\n \<title\>Time\</title\>\n \<script type=\"text/javascript\"\> \n \n var date = new Date().toString(…

---

<div class="post-metadata">

**Author:** ![Vincent78](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@Vincent78](https://discourse.nodered.org/u/Vincent78)\
**Post date:** [12 January 2021 08:31 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/3 "2021-01-12T08:31:54Z")

</div>

Hello UnborN,  
Thanks a lot for your answer. It's usefull. I just need to change timeStamp to string DateTime and I will be a happy man ; )  
Vincent

---

<div class="post-metadata">

**Author:** ![Vincent78](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@Vincent78](https://discourse.nodered.org/u/Vincent78)\
**Post date:** [12 January 2021 09:45 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/4 "2021-01-12T09:45:26Z")

</div>

I changed the TimeStamp in String with [$fromMillis](https://discourse.nodered.org/t/anyone-one-convert-timestamp-epoc-to-formatted-time-string-in-a-single-change-node/35482) but I have a problem with [TimeZone](https://discourse.nodered.org/t/convert-non-utc-timestamp-to-utc/16092/13).  
Vincent

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 January 2021 11:12 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/5 "2021-01-12T11:12:47Z")

</div>

Hi Vincent,

You can use the [Moment](https://flows.nodered.org/node/node-red-contrib-moment) node that is very useful in converting datetime in correct format including timezones.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8eb1db9b0521929f16eb1c07192d766dd205bb21.png)

Once you have the datetime in the correct format you send it to an Exec node with the command  
`sudo timedatectl set-time` and its important to have the Append checked so the dt will be appended to it.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/79bde618be96b15c8bfdc059ad5657e92b3c5bfd.png)

I havent tested it because i dont have a RPi setup now but here's a more complete flow :

```auto
[{"id":"4fbd5cb6.955534","type":"ui_template","z":"ac0f61dd.69e26","group":"c205e26f.6865a","name":"","order":0,"width":"0","height":"0","format":" <html>\n <head>\n <title>Time</title>\n <script type=\"text/javascript\"> \n \n var date = new Date().toString();\n document.getElementById('ct').innerHTML = date;\n \n this.scope.sendDate = function() {\n console.log(\"in function\")\n // return {payload: new Date().toString()}; // .valueOf() \n return {payload: new Date().valueOf()}; // \n }\n \n</script>\n \n </head>\n\n <body>\n <div id='ct' style=\"margin-bottom: 40px;\"></div>\n <br>\n <br>\n<md-button ng-click=\"send(sendDate())\">\n Send time to Node-red\n</md-button>\n\n </body>\n </html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":360,"y":820,"wires":[["193cbc9b.13c77b"]]},{"id":"1b156e9f.e82be9","type":"debug","z":"ac0f61dd.69e26","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1050,"y":820,"wires":[]},{"id":"193cbc9b.13c77b","type":"moment","z":"ac0f61dd.69e26","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Europe/London","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en-GB","output":"payload","outputType":"msg","outTz":"Europe/London","x":580,"y":820,"wires":[["1fdc2584.f79f72","4cd3096d.55811"]]},{"id":"1fdc2584.f79f72","type":"exec","z":"ac0f61dd.69e26","command":"sudo timedatectl set-time","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":850,"y":820,"wires":[["1b156e9f.e82be9"],[],[]]},{"id":"aeee1b9e.304af","type":"comment","z":"ac0f61dd.69e26","name":"sudo timedatectl set-time 'yyyy-mm-dd hh:mm:ss'","info":"","x":860,"y":740,"wires":[]},{"id":"4cd3096d.55811","type":"debug","z":"ac0f61dd.69e26","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":920,"wires":[]},{"id":"c205e26f.6865a","type":"ui_group","name":"Default","tab":"20af7edd.a91fca","order":1,"disp":true,"width":"12","collapse":false},{"id":"20af7edd.a91fca","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Vincent78](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@Vincent78](https://discourse.nodered.org/u/Vincent78)\
**Post date:** [12 January 2021 11:29 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/6 "2021-01-12T11:29:19Z")

</div>

I had problem to download catalogue. Without moment I created this code:

```auto
[
    {
        "id": "c4c4b1e9.2e3c8",
        "type": "exec",
        "z": "1e369b26.8cd9e5",
        "command": "sudo date -s ",
        "addpay": true,
        "append": "",
        "useSpawn": "false",
        "timer": "1",
        "oldrc": false,
        "name": "",
        "x": 1050,
        "y": 100,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "7a163e24.fc81b",
        "type": "ui_template",
        "z": "1e369b26.8cd9e5",
        "group": "6a269076.815ee",
        "name": "Clock Update",
        "order": 0,
        "width": "0",
        "height": "0",
        "format": "<html>\n <head>\n <title>Time</title>\n <script type=\"text/javascript\"> \n \n var date = new Date().toString();\n document.getElementById('ct').innerHTML = date;\n \n this.scope.sendDate = function() {\n console.log(\"in function\");\n //return {payload: new Date().toString()};\n //return {payload: new Date().valueOf()};\n \n var d = new Date();\n var year, month, day, hours, minutes, seconds; \n\n year = 1900+d.getYear();\n year = year.toString();\n month = 1+d.getMonth();\n month = month.toString();\n day = d.getDate();\n day = day.toString();\n hours = d.getHours();\n hours = hours.toString();\n minutes = d.getMinutes();\n minutes = minutes.toString();\n seconds = d.getSeconds();\n seconds = seconds.toString();\n\n return {payload: \"'\"+year+\"/\"+month+\"/\"+day+\" \"+hours+\":\"+minutes+\":\"+seconds+\"'\"};\n }\n </script>\n </head>\n\n <body>\n <div id='ct' style=\"margin-bottom: 40px;\"></div>\n <br>\n <br>\n <md-button ng-click=\"send(sendDate())\">\n Send time to Node-red\n </md-button>\n </body>\n \n</html>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "x": 860,
        "y": 40,
        "wires": [
            [
                "c4c4b1e9.2e3c8",
                "b13be7c1.360b58"
            ]
        ]
    },
    {
        "id": "b13be7c1.360b58",
        "type": "debug",
        "z": "1e369b26.8cd9e5",
        "name": "DateUser",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1040,
        "y": 40,
        "wires": []
    },
    {
        "id": "6a269076.815ee",
        "type": "ui_group",
        "z": "",
        "name": "Clock",
        "tab": "dab605fb.c8a088",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "dab605fb.c8a088",
        "type": "ui_tab",
        "z": "",
        "name": "Update Clock",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

```

Vincent

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [12 January 2021 11:52 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/7 "2021-01-12T11:52:14Z")

</div>

Maybe this ?

```auto
var d = new Date();
var year, month, day, hours, minutes, seconds; 

    year = d.getFullYear();

    month = 1+d.getMonth();
    month = month < 10 ? "0" + month : month // add leading 0
   
    day = d.getDate();
    day = day < 10 ? "0" + day : day // add leading 0
   
    hours = d.getHours();
    hours = hours < 10 ? "0" + hours : hours // add leading 0
   
    minutes = d.getMinutes();
    minutes = minutes < 10 ? "0" + minutes : minutes // add leading 0
  
    seconds = d.getSeconds();
    seconds = seconds < 10 ? "0" + seconds : seconds // add leading 0
   

    return {payload: `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`};

```

---

<div class="post-metadata">

**Author:** ![Vincent78](https://avatars.discourse-cdn.com/v4/letter/v/7ab992/32.png) [@Vincent78](https://discourse.nodered.org/u/Vincent78)\
**Post date:** [12 January 2021 12:24 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/8 "2021-01-12T12:24:12Z")

</div>

I created almost the same code. See above:

```auto
var d = new Date();
var year, month, day, hours, minutes, seconds;
year = 1900+d.getYear();
year = year.toString();
month = 1+d.getMonth();
month = month.toString();
day = d.getDate();
day = day.toString();
hours = d.getHours();
hours = hours.toString();
minutes = d.getMinutes();
minutes = minutes.toString();
seconds = d.getSeconds();
seconds = seconds.toString();
return {payload: \"'\"+year+\"/\"+month+\"/\"+day+\" \"+hours+\":\"+minutes+\":\"+seconds+\"'\"};

```

And Exec sudo date -s + payload

Thanks a lot UnborN

Vincent

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [13 January 2021 08:35 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/9 "2021-01-13T08:35:55Z")

</div>

Another way of converting the Datetime is using a Change node with a Jsonata expression  
the Moment library is loaded by default in Jsonata (forgot about it)

`$moment(msg.payload).format("YYYY-MM-DD HH:mm:ss")`

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0a8aaedf231504d9f970b2eb92384c10b2790e2.png)

**Sample Flow :**

```auto
[{"id":"1b56d6f7.0c1151","type":"change","z":"ac0f61dd.69e26","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(msg.payload).format(\"YYYY-MM-DD HH:mm:ss\")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":1040,"wires":[["4cd3096d.55811"]]},{"id":"77b16f17.c1b88","type":"debug","z":"ac0f61dd.69e26","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":350,"y":980,"wires":[]},{"id":"4fbd5cb6.955534","type":"ui_template","z":"ac0f61dd.69e26","group":"c205e26f.6865a","name":"","order":0,"width":"0","height":"0","format":" <html>\n <head>\n <title>Time</title>\n <script type=\"text/javascript\"> \n \n var date = new Date().toString();\n document.getElementById('ct').innerHTML = date;\n \n this.scope.sendDate = function() {\n console.log(\"in function\")\n // return {payload: new Date().toString()}; // .valueOf() \n return {payload: new Date().valueOf()}; // \n }\n \n</script>\n \n </head>\n\n <body>\n <div id='ct' style=\"margin-bottom: 40px;\"></div>\n <br>\n <br>\n<md-button ng-click=\"send(sendDate())\">\n Send time to Node-red\n</md-button>\n\n </body>\n </html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":220,"y":1040,"wires":[["77b16f17.c1b88","1b56d6f7.0c1151"]]},{"id":"4cd3096d.55811","type":"debug","z":"ac0f61dd.69e26","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":1040,"wires":[]},{"id":"c205e26f.6865a","type":"ui_group","name":"Default","tab":"20af7edd.a91fca","order":1,"disp":true,"width":"12","collapse":false},{"id":"20af7edd.a91fca","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 March 2021 08:36 UTC](https://discourse.nodered.org/t/update-rpi3-datetime-by-wifi/38955/10 "2021-03-14T08:36:07Z")

</div>

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