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:
<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>
@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.
<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>
<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:
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.
Thank you @TotallyInformation and @bakman2 for your inputs.
My data has the form '2019-11-29T00:00:00' and your commands
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
<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 link:
<!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
document.write();
works with this 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.
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.
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
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:
<th>{{new Date('2019-11-29T00:00:00')}}</th>
Full test code that does't work:
<!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>
@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?
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:
const d = new Date(msg.payload.daytime)
msg.payload.month = d.getMonth()
return msg