# Convert datetime - template

**URL:** <https://discourse.nodered.org/t/convert-datetime-template/18606>\
**Category:** General\
**Created:** [29 November 2019 12:24 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606 "2019-11-29T12:24:23Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [29 November 2019 12:24 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/1 "2019-11-29T12:24:23Z")

</div>

Hay!  
I try to convert a datetime (format: YYYY-MM-DDT00:00:00) from a json format and put it in a cell of a table to display it on the dashboard.  
It doesn't work and I don´t know how I get it.  
Sorry, I'm a beginner with java / html.  
Can someone help me?  
Any suggestions for improvement are more than welcome.  
Thanks you in advance!!!

My source code:

```auto
<table id="table" border="1">
    <tr>
        <script>
            function myFunction() {
            var d = new Date(msg.payload.daytime);
            var n = d.getMonth(); 
            }
        </script>
        
        <th>{{myFunction()}}</th>
        <th>{{msg.payload.Forecasts[0].status}}</th>
        
    </tr>
    

</table>

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 November 2019 17:35 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/2 "2019-11-29T17:35:12Z")

</div>

Your script tags are in the wrong place for starters.

Assuming that is in a Dashboard template node, put the script before the `<tr>` tag.

The second problem is that you are not returning anything from your JavaScript function so there is nothing to display.

```auto
        <script>
            function myFunction() {
                var d = new Date(msg.payload.daytime);
                var n = d.getMonth(); 
                return n;
            }
        </script>

```

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [29 November 2019 18:52 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/3 "2019-11-29T18:52:24Z")

</div>

@TotallyInformation:  
Thank you for your help.  
Now I canched the source code, but it doesn't work.  
Even I change the command "new Date(msg.payload.daytime);" to "new Date(2019-11-29T00:00:00);" as a example.

```auto
<table id="table" border="1">
  <script>
            function myFunction() {
            var d = new Date(msg.payload.daytime);
            var n = d.getMonth(); 
            return n;
            }
    </script>   
 <tr>
        <th>{{myFunction()}}</th>
        <th>{{msg.payload.Forecasts[0].status}}</th>
        
    </tr>
    

</table>

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 November 2019 19:11 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/4 "2019-11-29T19:11:51Z")

</div>

```auto
<script>
    function myFunction() {
        var d = new Date(msg.payload.daytime);
        var n = d.getMonth(); 
        return n;
    }
</script>   

<table id="table" border="1">
    <tr>
        <th>{{myFunction()}}</th>
        <th>{{msg.payload.Forecasts[0].status}}</th>
    </tr>
</table>

```

Don't put script tags inside other tags, I'm not sure whether all browsers will treat that correctly.

The date/time string should really have a "Z" on the end to make it a proper ISO format. Or another timezone if it isn't Zulu time (UTC).

However, the following works correctly:

```auto
console.log(new Date('2019-11-29T00:00:00'))
console.log( (new Date('2019-11-29T00:00:00')).getMonth() )

```

So I suggest you double check what you are actually sending in the msg since it may be invalid.

Open you're browsers developer console to see if there are errors.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [29 November 2019 19:14 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/5 "2019-11-29T19:14:10Z")

</div>

Why not use a moment node and send the output to the dashboard ?

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [1 December 2019 12:15 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/6 "2019-12-01T12:15:11Z")

</div>

Thank you @TotallyInformation and @bakman2 for your inputs.  
My data has the form '2019-11-29T00:00:00' and your commands

```auto
console.log(new Date('2019-11-29T00:00:00'))
console.log( (new Date('2019-11-29T00:00:00')).getMonth() )

```

works perfect.  
Now with the command

```auto
<th><script>test();</script></th>

```

I was able to call my function. But I don't get a return value from the function. What I did wrong? My full test code wich I test it with [this](https://www.w3schools.com/default.asp) link:

```auto
<!DOCTYPE html>
<html>
<body>

<h2>My First JavaScript</h2>

 <script>
function test()	{
	var d = new Date('2019-11-10T00:00:00').getMonth();
        // document.write(d);         
    console.log(d);
    return d;
}
</script> 

<table>
	<tr>
    	<th>abc</th>
        <th><script>test();</script></th>
    <tr>   
    
    
   
</table>

</body>
</html> 

```

The command

```auto
document.write();

```

works with [this](https://www.w3schools.com/default.asp) link. But in my Node Red Dashboard it creats a new empty site with the output but my gauge, charts,... aren't present.

@bakman2:  
The moment node is a good idea. I get do full not only one cell in the table but diverse data wich a riceive in the json-format. And so I thought do this with javascript.

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [1 December 2019 16:51 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/7 "2019-12-01T16:51:40Z")

</div>

has nobody an idea?😨

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 December 2019 16:58 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/8 "2019-12-01T16:58:50Z")

</div>

You didn't really pay full attention to what I wrote before. I suggest checking your code against mine.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 December 2019 16:59 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/9 "2019-12-01T16:59:45Z")

</div>

> [@EL\_tech](#):
>
> has nobody an idea?

We aren't paid support you know and it happens to be the weekend.

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [1 December 2019 17:17 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/10 "2019-12-01T17:17:33Z")

</div>

Sorry, It wos only a open question and not a critic.  
Find it great if your expert helps us lend for free.  
I am a beginner and unfortunately I do not understand everything at the begin and not everthink work at the begin... Sorry.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [1 December 2019 21:56 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/11 "2019-12-01T21:56:50Z")

</div>

OK, just check your code against mine to see the difference which should let you correct your issues.

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [2 December 2019 18:52 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/12 "2019-12-02T18:52:38Z")

</div>

@TotallyInformation:  
Thank you for your helpfulness!

Yes, I know, that the two code are different. With your code I saw that the function "myfunction" isn't called. I don't know what i do wrong...  
The commands

```auto
console.log(new Date('2019-11-29T00:00:00'))
console.log( (new Date('2019-11-29T00:00:00')).getMonth() )

```

works.  
I don't know how I can call a function in a table (cell) and get the return value in the cell....  
This not work, too:

```auto
<th>{{new Date('2019-11-29T00:00:00')}}</th>

```

Full test code that does't work:

```auto
<!DOCTYPE html>
<html>
<body>

<h2>My First JavaScript</h2>

 <script>
function test()	{
	var d = new Date('2019-11-10T00:00:00').getMonth();
        // document.write(d);         
    console.log(d);
    return d;
}
</script> 

<table>
	<tr>
    	<th>abc</th>
        <th>{{new Date('2019-11-29T00:00:00').getMonth()}}</th>
    <tr>   
    
    
   
</table>

</body>
</html> 

```

Thank you so much for your assistance!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 December 2019 20:22 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/13 "2019-12-02T20:22:05Z")

</div>

Like I've probably said, I really don't use Angular any more as it is too painful.

But a quick online search gave the answer:

[https://www.startpage.com/do/dsearch?query=angular+1+show+results+of+function](https://www.startpage.com/do/dsearch?query=angular+1+show+results+of+function)

> <https://stackoverflow.com/questions/25719870/angular-output-from-function-on-expression>

Of course, using that with Dashboard isn't so easy but my blog has some clues:

> **[Node-RED Dashboard Template Examples (AngularJS)](https://it.knightnet.org.uk/kb/nr-qa/dashboard-template-examples/)**
>
> Some examples of using AngularJS with the Node-RED Dashboard.
> Not all features of Angular are accessible via the Dashboard.

Putting that together:

```auto
<div>
    {{test(msg.payload)}}
</div>

<script>
    // Lambda function to access the Angular Scope
    ;(function(scope) {
        scope.test = function(date) {
            return new Date().getMonth()
        }
    })(scope)
</script>

```

And now you know why I wrote uibuilder 😀

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [3 December 2019 19:12 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/14 "2019-12-03T19:12:11Z")

</div>

@TotallyInformation:  
Thank you so much for your help.😉🕶  
I will read your inputs a try to realize it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2019 20:20 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/15 "2019-12-03T20:20:18Z")

</div>

Might it be easier to determine the date in node-red and pass that to the dashboard?

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [4 December 2019 16:25 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/16 "2019-12-04T16:25:42Z")

</div>

@Colin:  
Thank you for your input!  
Yes that' true.  
But:  
I have a table which is filled with data from a website (json format).  
How can I add the calculated values ​​(in node red) in this table?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 December 2019 17:20 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/17 "2019-12-04T17:20:02Z")

</div>

Where there's JSON and Node-RED, you already have a solution 😀

JSON is processable directly by JavaScript so where you have the little function to get the month, you can simply add the month to the msg object in Node-RED. In a function node for example:

```auto
const d = new Date(msg.payload.daytime)

msg.payload.month = d.getMonth()

return msg

```

---

<div class="post-metadata">

**Author:** ![EL\_tech](https://avatars.discourse-cdn.com/v4/letter/e/258eb7/32.png) [@EL\_tech](https://discourse.nodered.org/u/EL_tech)\
**Post date:** [4 December 2019 18:27 UTC](https://discourse.nodered.org/t/convert-datetime-template/18606/18 "2019-12-04T18:27:20Z")

</div>

@Colin @TotallyInformation @bakman2:  
Thank you so much for your inputs.  
I thought too complicated.  
Thank you @TotallyInformation for your super example! It works!!!  
I'm happy! 🕶 🤠
