赞
踩
在实际操作中,我们从前端传递时,还是中国时间,但是到了node-red中,就少了8小时,出现时区不对应的情况。
Node-RED默认使用UTC时区,如果想更改时区,你可以在运行Node-RED时通过TZ变量来传入你期望的时区。
node-red修改时区有两种方法,一种是通过moment节点修改,一种是通过date函数修改。
1. 方法对比如下:
2. 总结:
对于大数据量的数组循环批量时,推荐第二种
;对于单次或少量的转换时,推荐第一种
。方法详情如下:
node-red-contrib-moment
节点moment节点详细操作参考链接:读取时间的节点 node-red-contrib-moment
[ { "id": "48a7566649d1f1b7", "type": "inject", "z": "411470ea6ab023d5", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 170, "y": 480, "wires": [ [ "9a41e67fee63eab8" ] ] }, { "id": "9a41e67fee63eab8", "type": "function", "z": "411470ea6ab023d5", "name": "初始化", "func": "msg.data = [[\"2023-07-26T07:53:50.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:48.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:46.000Z\", 449, 0, 0, 0, 1]];\nfor(let i = 0;i <10000; i++){\n msg.data.push([\"2023-07-26T07:53:50.000Z\", 4+i, 0, 0, 0, 1]);\n}\nmsg.t = new Date().getTime();\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 290, "y": 480, "wires": [ [ "b8d4ba7dcd1ea9cd", "48cc9a3e33f41101" ] ] }, { "id": "b8d4ba7dcd1ea9cd", "type": "debug", "z": "411470ea6ab023d5", "name": "debug 35", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "data", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 380, "y": 660, "wires": [] }, { "id": "48cc9a3e33f41101", "type": "array-loop", "z": "411470ea6ab023d5", "name": "", "key": "id", "keyType": "msg", "reset": true, "resetValue": "value-undefined", "array": "data", "arrayType": "msg", "x": 460, "y": 520, "wires": [ [ "ebfe46536b1e2467" ], [ "3a2d7148ba39a821" ] ] }, { "id": "5cfd0bcf779ca93b", "type": "moment", "z": "411470ea6ab023d5", "name": "时区转换", "topic": "", "input": "dateStr", "format": "YYYY-MM-DD HH:mm:ss.SSS", "locale": "en-US", "output": "dateStr", "x": 560, "y": 600, "wires": [ [ "6e8f4da78a919e81" ] ] }, { "id": "ebfe46536b1e2467", "type": "function", "z": "411470ea6ab023d5", "name": "function 7", "func": "msg.t = \"耗时:\" + ((new Date().getTime() - msg.t) / 1000) + \"秒\";\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 640, "y": 520, "wires": [ [ "f46f8436d1d35a36" ] ] }, { "id": "f46f8436d1d35a36", "type": "debug", "z": "411470ea6ab023d5", "name": "debug 37", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 780, "y": 520, "wires": [] }, { "id": "3a2d7148ba39a821", "type": "function", "z": "411470ea6ab023d5", "name": "function 8", "func": "msg.dateStr = msg.data[msg.id][0].toUpperCase();\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 420, "y": 600, "wires": [ [ "5cfd0bcf779ca93b" ] ] }, { "id": "6e8f4da78a919e81", "type": "function", "z": "411470ea6ab023d5", "name": "function 9", "func": "msg.data[msg.id][0] = msg.dateStr;\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 700, "y": 600, "wires": [ [ "48cc9a3e33f41101" ] ] } ]
var d = new Date(dateStr)
d.setHours(d.getHours() + 8);//将时加8
let s = d.toISOString().replace("T", " ").replace("Z", "");//去掉S T的时间格式
示例js代码如下:
[ { "id": "b8d4ba7dcd1ea9cd", "type": "debug", "z": "411470ea6ab023d5", "name": "debug 35", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "data", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 480, "y": 460, "wires": [] }, { "id": "b8a9fec4858d0409", "type": "function", "z": "411470ea6ab023d5", "name": "转换", "func": "msg.data.forEach((item) => {\n if (item[0].includes(\"T\")){\n let d = new Date(item[0]);\n d.setHours(d.getHours() + 8);\n item[0] = d.toISOString().replace(\"T\", \" \").replace(\"Z\", \"\");\n }\n});\nmsg.t = \"耗时:\" + ((new Date().getTime() - msg.t) / 1000)+\"秒\";\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 430, "y": 400, "wires": [ [ "e18227c520476fe0" ] ] }, { "id": "e18227c520476fe0", "type": "debug", "z": "411470ea6ab023d5", "name": "debug 36", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "true", "targetType": "full", "statusVal": "", "statusType": "auto", "x": 600, "y": 400, "wires": [] }, { "id": "9a41e67fee63eab8", "type": "function", "z": "411470ea6ab023d5", "name": "初始化", "func": "msg.data = [[\"2023-07-26T07:53:50.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:48.000Z\", 449, 0, 0, 0, 1], [\"2023-07-26T07:53:46.000Z\", 449, 0, 0, 0, 1]];\nfor(let i = 0;i <10000; i++){\n msg.data.push([\"2023-07-26T07:53:50.000Z\", 4+i, 0, 0, 0, 1]);\n}\nmsg.t = new Date().getTime();\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 290, "y": 480, "wires": [ [ "b8d4ba7dcd1ea9cd", "b8a9fec4858d0409" ] ] }, { "id": "48a7566649d1f1b7", "type": "inject", "z": "411470ea6ab023d5", "name": "", "props": [ { "p": "payload" }, { "p": "topic", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "onceDelay": 0.1, "topic": "", "payload": "", "payloadType": "date", "x": 170, "y": 480, "wires": [ [ "9a41e67fee63eab8" ] ] } ]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。