#日付・時刻の追加
- Node-REDのフローで日付・時刻を追加します。
- Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。
日付・時刻の追加
[{"id":"14eb82eb.c439dd","type":"inject","z":"b45c9d8e.e65048","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":123,"y":160,"wires":[["2a5f2f01.947d7"]]},{"id":"2a5f2f01.947d7","type":"function","z":"b45c9d8e.e65048","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\nvar minute = dt.getMinutes();\nvar second = dt.getSeconds();\nif (!msg.payload) {\n msg.payload = { \"initial\": \"value\" };\n}\nmsg.payload.date = {\n \"date\": date,\n \"year\": year,\n \"month\": month,\n \"day\": day,\n \"hour\": hour,\n \"minute\": minute,\n \"second\": second\n};\nreturn msg;","outputs":1,"noerr":0,"x":351,"y":160,"wires":[["6d9d59d7.6b8c78","c59ca39c.9b9138","729fbad4.d3b874","a21801c7.9091f8","96f9501a.b885e","10bdd44a.74e04c"]]},{"id":"6d9d59d7.6b8c78","type":"debug","z":"b45c9d8e.e65048","name":"","active":false,"console":"false","complete":"true","x":583,"y":160,"wires":[]},{"id":"c59ca39c.9b9138","type":"debug","z":"b45c9d8e.e65048","name":"","active":false,"console":"false","complete":"payload.d.temp","x":631,"y":300,"wires":[]},{"id":"729fbad4.d3b874","type":"debug","z":"b45c9d8e.e65048","name":"","active":false,"console":"false","complete":"payload","x":601,"y":200,"wires":[]},{"id":"7fb8f08d.128b2","type":"delay","z":"b45c9d8e.e65048","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":231,"y":80,"wires":[["2a5f2f01.947d7"]]},{"id":"a21801c7.9091f8","type":"debug","z":"b45c9d8e.e65048","name":"","active":false,"console":"false","complete":"payload.date.day","x":631,"y":400,"wires":[]},{"id":"96f9501a.b885e","type":"debug","z":"b45c9d8e.e65048","name":"","active":false,"console":"false","complete":"payload.d","x":611,"y":260,"wires":[]},{"id":"10bdd44a.74e04c","type":"debug","z":"b45c9d8e.e65048","name":"","active":false,"console":"false","complete":"payload.date","x":621,"y":360,"wires":[]}]
- フロー左端のinjectノードのボタンを押して出力を確認します。
- 「日付・時刻の追加」functionノードの内容は以下の通りです。
日付・時刻の追加
var d = new Date();
var localTime = d.getTime();
var localOffset = d.getTimezoneOffset() * 60000;
var utc = localTime + localOffset;
var offset = -9.0;
var result = utc - (3600000 * offset);
var date = parseInt(new Date(result) / 1000);
var dt = new Date(result);
var year = dt.getFullYear();
var month = dt.getMonth()+1;
var day = dt.getDate();
var hour = dt.getHours();
var minute = dt.getMinutes();
var second = dt.getSeconds();
if (!msg.payload) {
msg.payload = { "initial": "value" };
}
msg.payload.date = {
"date": date,
"year": year,
"month": month,
"day": day,
"hour": hour,
"minute": minute,
"second": second
};
return msg;