『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 2914|回复: 11

[教程系列] 2023、2024年Node-RED判决是节假日、工作日函数

[复制链接]

5

主题

90

帖子

1542

积分

金牌会员

Rank: 6Rank: 6

积分
1542
金钱
1452
HASS币
0
发表于 2023-11-9 09:52:37 | 显示全部楼层 |阅读模式
本帖最后由 liaozhihong 于 2023-11-9 10:49 编辑

无聊中看到看到大家在找判断是否节假日还是工作日的流,看大佬们写的太复杂了,来个简单的,最后以一个函数的形式体现出来,如果有BUG的话,希望指正。
代码如下:

// 创建一个表示当前日期和时间的对象
var now = new Date();
// 获取年份
let year = now.getFullYear();
// 获取月份(注意月份是从0开始计数的,所以需要加1)
let month = now.getMonth() + 1;
// 获取日期
let day = now.getDate();
// 格式化为 YYYY-MM-DD  如2023-01-01
let formattedDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;

var sentence = "";

//节假日   日期用  "https://timor.tech/api/holiday/year/2024"  用这个连接获取
//定义2023年放假时间:前面的时间过完了,就没有添加了
var yeararr = ['2023-12-30', '2023-12-31'];

//定义2024年节假日时间:   如果是2024年了,就把上面的2023的删除了,用下面的数组就行了
/*
var yeararr = [
    '2024-01-01', '2024-02-04', '2024-02-10', '2024-02-11', '2024-02-12', '2024-02-13',
    '2024-02-14', '2024-02-15', '2024-02-16', '2024-02-17', '2024-02-18', '2024-04-04',
    '2024-04-05', '2024-04-06', '2024-04-07', '2024-04-28', '2024-05-01', '2024-05-02',
    '2024-05-03', '2024-05-04', '2024-05-05', '2024-05-11', '2024-06-08', '2024-06-09',
    '2024-06-10', '2024-09-14', '2024-09-15', '2024-09-16', '2024-09-17', '2024-09-29',
    '2024-10-01', '2024-10-02', '2024-10-03', '2024-10-04', '2024-10-05', '2024-10-06',
    '2024-10-07', '2024-10-12'];
*/

    // 判断是否为节假日
if (yeararr.indexOf(formattedDate) != -1) {
    // 如果是周末,则将"sentence"变量设置为"节假日"
    sentence = "休息日";
    // 判断是否为工作日,如果当前星期几在1到5之间(包括1和5),则为工作日
} else if (now.getDay() >= 1 && now.getDay() <= 5) {
    // 如果是周末,则将"sentence"变量设置为"工作日"
    sentence = "工作日";
    // 判断是否为周末,如果当前星期几为0或6,则为周末
} else if (now.getDay() === 0 || now.getDay() === 6) {
    // 如果是周末,则将"sentence"变量设置为"休息日"
    sentence = "休息日";
}
// 将结果存储在msg.payload对象中的speak属性中
msg.payload = { speak: sentence };
// 返回修改后的msg对象
return msg;
函数输出也可以做二路输出:在后面加一个IF语句:
if (sentence == "工作日") {
    return [msg, null];
} else if (sentence == "休息日") {
    return [null, msg];
}

效果为这样:
QQ图片20231109104431.png
上面作为工作日,下面做为休息日要处理的事件。

,2023年过后,就使用2024年的那个数组,把2023的数组删了就行了。祝大家玩的开心
下面加上流代码:
[{"id":"cb3dab61683c7148","type":"function","z":"b0d47086b3d0620e","name":"判断是否节假日函数","func":"\n// 创建一个表示当前日期和时间的对象\nvar now = new Date();\n// 获取年份\nlet year = now.getFullYear();\n// 获取月份(注意月份是从0开始计数的,所以需要加1)\nlet month = now.getMonth() + 1;\n// 获取日期\nlet day = now.getDate();\n// 格式化为 YYYY-MM-DD  如2023-01-01\nlet formattedDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;\n\nvar sentence = "";\n\n//节假日   日期用  "https://timor.tech/api/holiday/year/2024"  用这个连接获取\n//定义2023年放假时间:前面的时间过完了,就没有添加了\nvar yeararr = ['2023-12-30', '2023-12-31'];\n\n//定义2024年节假日时间:   如果是2024年了,就把上面的2023的删除了,用下面的数组就行了\n/*\nvar yeararr = [\n    '2024-01-01', '2024-02-04', '2024-02-10', '2024-02-11', '2024-02-12', '2024-02-13',\n    '2024-02-14', '2024-02-15', '2024-02-16', '2024-02-17', '2024-02-18', '2024-04-04',\n    '2024-04-05', '2024-04-06', '2024-04-07', '2024-04-28', '2024-05-01', '2024-05-02',\n    '2024-05-03', '2024-05-04', '2024-05-05', '2024-05-11', '2024-06-08', '2024-06-09',\n    '2024-06-10', '2024-09-14', '2024-09-15', '2024-09-16', '2024-09-17', '2024-09-29',\n    '2024-10-01', '2024-10-02', '2024-10-03', '2024-10-04', '2024-10-05', '2024-10-06',\n    '2024-10-07', '2024-10-12'];\n*/\n\n    // 判断是否为节假日\nif (yeararr.indexOf(formattedDate) != -1) {\n    // 如果是周末,则将"sentence"变量设置为"节假日"\n    sentence = "休息日";\n    // 判断是否为工作日,如果当前星期几在1到5之间(包括1和5),则为工作日\n} else if (now.getDay() >= 1 && now.getDay() <= 5) {\n    // 如果是周末,则将"sentence"变量设置为"工作日"\n    sentence = "工作日";\n    // 判断是否为周末,如果当前星期几为0或6,则为周末\n} else if (now.getDay() === 0 || now.getDay() === 6) {\n    // 如果是周末,则将"sentence"变量设置为"休息日"\n    sentence = "休息日";\n}\n// 将结果存储在msg.payload对象中的speak属性中\nmsg.payload = { speak: sentence };\n// 返回修改后的msg对象\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":400,"y":1240,"wires":[["c1c4ced5077564cf"]]},{"id":"e30ecc986f8fe5f1","type":"inject","z":"b0d47086b3d0620e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":1240,"wires":[["cb3dab61683c7148"]]},{"id":"c1c4ced5077564cf","type":"debug","z":"b0d47086b3d0620e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":1240,"wires":[]}]



回复

使用道具 举报

22

主题

622

帖子

2813

积分

金牌会员

Rank: 6Rank: 6

积分
2813
金钱
2191
HASS币
0
发表于 2023-11-9 10:49:45 | 显示全部楼层
加上特殊日期倒计时的提醒内容该怎么加?
回复

使用道具 举报

5

主题

90

帖子

1542

积分

金牌会员

Rank: 6Rank: 6

积分
1542
金钱
1452
HASS币
0
 楼主| 发表于 2023-11-9 10:52:37 | 显示全部楼层
a_dongde 发表于 2023-11-9 10:49
加上特殊日期倒计时的提醒内容该怎么加?

我感觉做一个日期计算,计算出还有多久,再加上一个判断应该就可以了吧
回复

使用道具 举报

3

主题

47

帖子

317

积分

中级会员

Rank: 3Rank: 3

积分
317
金钱
270
HASS币
0
发表于 2023-11-9 12:51:16 | 显示全部楼层
666
回复

使用道具 举报

8

主题

138

帖子

1952

积分

论坛积极会员

积分
1952
金钱
1814
HASS币
0
发表于 2023-11-9 13:48:34 | 显示全部楼层
回复

使用道具 举报

0

主题

85

帖子

693

积分

高级会员

Rank: 4

积分
693
金钱
608
HASS币
0
发表于 2023-11-9 15:38:30 | 显示全部楼层
直接调用HA的也方便
回复

使用道具 举报

10

主题

680

帖子

5702

积分

论坛元老

Rank: 8Rank: 8

积分
5702
金钱
5017
HASS币
50
发表于 2023-11-9 23:32:36 | 显示全部楼层
回复

使用道具 举报

5

主题

44

帖子

332

积分

中级会员

Rank: 3Rank: 3

积分
332
金钱
288
HASS币
0
发表于 2024-2-18 17:10:15 | 显示全部楼层
没考虑调休等情况, 我是用的 https://www.tianapi.com/apiview/139 这个, 每天写到input_boolean里去
回复

使用道具 举报

34

主题

189

帖子

896

积分

高级会员

Rank: 4

积分
896
金钱
707
HASS币
0
发表于 2024-5-13 00:34:17 | 显示全部楼层
本帖最后由 yun59t 于 2024-5-15 12:50 编辑

没考虑周末调休成工作日的情况,自己乱改了一通,不懂行不行,有懂的大佬帮忙看看,直接复制到node red里导入中粘贴导入即可:
[{"id":"4940f5f149be677c","type":"tab","label":"工作日、休息日判定","disabled":false,"info":"","env":[]},{"id":"cb3dab61683c7148","type":"function","z":"4940f5f149be677c","name":"判断是否节假日函数","func":"// 创建一个表示当前日期和时间的对象\nvar now = new Date();\n\n// 从now中获取当前的年份,并存储到变量year中\nlet year = now.getFullYear();\n\n// 从now中获取月份(JavaScript的月份是从0开始计数,所以加1)\nlet month = now.getMonth() + 1;\n\n// 从now中获取日期\nlet day = now.getDate();\n\n// 将year、month、day合成字符串YYYY-MM-DD,为今天日期\nlet formattedDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;\n\n// 初始化一个空字符串变量sentence\nvar sentence = "";\n\n//节假日的日期用"https://timor.tech/api/holiday/year/2024"用这个连接获取\n// 人工填写2024年节假日日期\nvar holidayArr = [\n '2024-01-01', '2024-02-04', '2024-02-10', '2024-02-11', '2024-02-12', '2024-02-13',\n '2024-02-14', '2024-02-15', '2024-02-16', '2024-02-17', '2024-02-18', '2024-04-04',\n '2024-04-05', '2024-04-06', '2024-04-07', '2024-04-28', '2024-05-01', '2024-05-02',\n '2024-05-03', '2024-05-04', '2024-05-05', '2024-05-11', '2024-06-08', '2024-06-09',\n '2024-06-10', '2024-09-14', '2024-09-15', '2024-09-16', '2024-09-17', '2024-09-29',\n '2024-10-01', '2024-10-02', '2024-10-03', '2024-10-04', '2024-10-05', '2024-10-06',\n '2024-10-07', '2024-10-12'];\n\n// 人工填写2024年调休日=工作日,即因调休而需要上班的周末日期\nvar workdayArr = [\n '2024-01-13', '2024-02-24', '2024-04-28', '2024-05-04', '2024-06-08',\n '2024-09-14', '2024-09-29', '2024-10-12'];\n\n// 判断是否为节假日: 检查“今天日期”是否在holidayArr假日数组中。在假日中,就将"sentence"变量设为"休息日"\nif (holidayArr.indexOf(formattedDate) != -1) { \n    sentence = "休息日";\n    \n    // 判断是否为调休日:若当前日期在workdayArr调休日数组中,则为工作日\n} else if (workdayArr.indexOf(formattedDate) != -1) {\n    sentence = "工作日";\n    \n    // 判断是否工作日:若当前是周一至周五,则为工作日。否则,设置为休息日\n} else if (now.getDay() >= 1 && now.getDay() <= 5) {\n    sentence = "工作日";\n    \n    // 判断是否为周末:若当前是周六或周日,并且不在workdayArr调休日数组中,则为休息日\n} else if (now.getDay() === 0 || now.getDay() === 6) {\n    sentence = "休息日";\n}\n\n// 将结果存储在msg.payload对象中的speak属性中\nmsg.payload = { speak: sentence };\n\n// 返回修改后的msg对象\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":320,"wires":[["c1c4ced5077564cf","6d8dbc875f33c171"]]},{"id":"e30ecc986f8fe5f1","type":"inject","z":"4940f5f149be677c","name":"定时循环","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"0 0-6 * * *","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":410,"y":320,"wires":[["cb3dab61683c7148","9a8f45908d1cdd73"]]},{"id":"c1c4ced5077564cf","type":"debug","z":"4940f5f149be677c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":830,"y":320,"wires":[]},{"id":"6d8dbc875f33c171","type":"ha-sensor","z":"4940f5f149be677c","name":"工作日或休息日","entityConfig":"00dfe422141207c8","version":0,"state":"payload.speak","stateType":"msg","attributes":[],"inputOverride":"allow","outputProperties":[],"x":840,"y":380,"wires":[[]]},{"id":"9a8f45908d1cdd73","type":"debug","z":"4940f5f149be677c","name":"debug 245","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":420,"wires":[]},{"id":"00dfe422141207c8","type":"ha-entity-config","z":"4940f5f149be677c","server":"9b347904.408af8","deviceConfig":"8022c011c0c13686","name":"2024工作日或休息日","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"工作日判定"},{"property":"icon","value":"mdi:briefcase"},{"property":"entity_picture","value":""},{"property":"entity_category","value":""},{"property":"device_class","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""}],"resend":true,"debugEnabled":true},{"id":"9b347904.408af8","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"时间:","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m:s.ms","enableGlobalContextStore":true},{"id":"8022c011c0c13686","type":"ha-device-config","z":"4940f5f149be677c","name":"2024工作日判定","hwVersion":"","manufacturer":"Node-RED","model":"","swVersion":""}]


回复

使用道具 举报

0

主题

20

帖子

131

积分

注册会员

Rank: 2

积分
131
金钱
111
HASS币
0
发表于 2024-5-24 15:14:14 | 显示全部楼层
smartws2016 发表于 2023-11-9 13:48
https://www.home-assistant.io/integrations/workday 直接调用HA的也方便

大神,这个对于调休好像有问题。我想判断第二天是否工作日,遇到周末上班的好像不行?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-22 11:04 , Processed in 0.085301 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表