Skip to main content

Calculate the number of working days in the form

1. Usage scenarios

In some cases, we need to calculate the number of legal working days within two specific date ranges.

2. Implement functions

2.1. Configuration Table sheet page

2.2. Calculate how many workdays are there between two dates

Copy the following code to the JS panel.

/**
* 计算两个日期之间有多少工作日
* @param {string} startTime 开始日期
* @param {string} endTime 结束日期
* @param {array} chinsesHolidays 中国特殊节假日(调休、补班)
*/
export function getWorkdaysCount(startTime = '', endTime = '', chinsesHolidays = []) {
let currentDate = new Date(startTime);
const endDate = new Date(endTime);
if (currentDate > endDate) {
return 0;
}
let workdaysCount = 0;
while (currentDate <= endDate) {
let dayOfWeek = currentDate.getDay();
let currentDateString = this.utils.formatter('date', currentDate.getTime(), 'YYYY-MM-DD');
if (dayOfWeek > 0 && dayOfWeek < 6) {
// 星期一 - 星期五
if (!chinsesHolidays.includes(currentDateString)) {
workdaysCount++;
}
} else {
// 星期六 - 星期日
if (chinsesHolidays.includes(currentDateString)) {
workdaysCount++;
}
}
currentDate.setDate(currentDate.getDate() + 1);
}
return workdaysCount;
}

After pasting, the effect is as follows:

2.3. Date component usage

Copy the following code to the page JS and bind it to the date component (both of which must be bound. Note that you need to modify the unique identifier of the component).

export function onChange({ value }) {
const startDate = this.utils.formatter('date', this.$('< 开始日期 >').getValue(), 'YYYY-MM-DD');
const endDate = this.utils.formatter('date', this.$('< 结束日期 >').getValue(), 'YYYY-MM-DD');
const specialHolidays = [
'2024-01-01',
'2024-02-04',
'2024-02-12',
'2024-02-13',
'2024-02-14',
'2024-02-15',
'2024-02-16',
'2024-02-18',
'2024-04-04',
'2024-04-05',
'2024-04-07',
'2024-04-28',
'2024-05-01',
'2024-05-02',
'2024-05-03',
'2024-05-11',
'2024-06-10',
'2024-09-14',
'2024-09-17',
'2024-09-29',
'2024-10-01',
'2024-10-02',
'2024-10-03',
'2024-10-04',
'2024-10-07',
'2024-10-12',
]; // 2024法定节假日日期
const workdays = this.getWorkdaysCount(startDate, endDate, specialHolidays);
this.$('< 工作日天数 >').setValue(workdays);
}

Note: 1,The specialHolidays parameter contains the annual holiday date, but does not include the normal weekend date. For example, 2024.05.04 is a weekend time and is also a special legal holiday, you only need to fill in the date of the rest from Monday to Friday this year or the date of working on Saturday and Sunday. 2. Due to different legal holidays each year, you need to manually maintain the annual legal holidays.

3. Effect

4. Try it online

This doc is generated using machine translation. Any discrepancies or differences created in the translation are not binding and have no legal effect for compliance or enforcement purposes.
Copyright © 2024钉钉(中国)信息技术有限公司和/或其关联公司浙ICP备18037475号-4