当前位置:   article > 正文

node-red时区问题解决方案对比_nodered判断当前时间

nodered判断当前时间

一、问题描述

  在实际操作中,我们从前端传递时,还是中国时间,但是到了node-red中,就少了8小时,出现时区不对应的情况。

  • 确认node-red时区:在node-red中输出当前时间,发现Node-red的容器内时间正常,Node-red输出的时间戳也是当前的时间。排除node-red本身时区问题。

  Node-RED默认使用UTC时区,如果想更改时区,你可以在运行Node-RED时通过TZ变量来传入你期望的时区。

  • 在数据库端查询,查看查询输出结果,结果正常,排除数据库本身时区设置问题。
  • 通过node-red使用http://rest查询tdengine数据库,返回的时间格式与实际时间少了8小时。需要修改时区。

回到目录

二、node-red修改时区的两种方法

  node-red修改时区有两种方法,一种是通过moment节点修改,一种是通过date函数修改。

1. 方法对比如下:

  • 第一种方式更通用,能支持多种格式的互相转换,但是转换速度慢,对于大数据量数组的转换需要消耗较长时间和资源。(批量转换10万条数据时,耗时0.5秒)
  • 第二种方式适应性较差,需要指定固定的时区进行转换,但是转换速度快,效率高。(批量转换1万条数据时,耗时1.4秒)

2. 总结:

  • 对于大数据量的数组循环批量时,推荐第二种
  • 对于单次或少量的转换时,推荐第一种

方法详情如下:

2.1 通过安装moment节点修改 - 少量时选择

  1. 安装node-red-contrib-moment节点
  2. 使用moment节点,将时间字符串进行转换,格式示例如下:
    1

moment节点详细操作参考链接:读取时间的节点 node-red-contrib-moment

  1. js代码详情:
[
    {
        "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"
            ]
        ]
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182

回到目录

2.2 通过date函数修改 - 批量时选择

var d = new Date(dateStr)
d.setHours(d.getHours() + 8);//将时加8
let s = d.toISOString().replace("T", " ").replace("Z", "");//去掉S T的时间格式
  • 1
  • 2
  • 3

示例js代码如下:
1

[
    {
        "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"
            ]
        ]
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104

回到目录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/218079?site
推荐阅读
相关标签
  

闽ICP备14008679号