#Countdown to a specific date - but only count weekdays

1 messages ยท Page 1 of 1 (latest)

pine summit
#

How do I count number of days until I go on vacation ๐Ÿ˜„

#

weekdays only

pine summit
#

For others:

// Define the target date
const targetDate = new Date('2025-03-12');
const currentDate = new Date(msg.payload);

// Ensure currentDate is midnight for consistency
currentDate.setHours(0, 0, 0, 0);

let weekdaysCount = 0;

// Loop through all days between currentDate and targetDate
for (let d = new Date(currentDate); d <= targetDate; d.setDate(d.getDate() + 1)) {
    const dayOfWeek = d.getDay();
    if (dayOfWeek !== 0 && dayOfWeek !== 6) { // Exclude Sundays (0) and Saturdays (6)
        weekdaysCount++;
    }
}

// Pass the result
msg.payload = weekdaysCount;
return msg;
vale jay
#

You could do this cleaner by working out the full number of weeks between your 2 dates, multiply it by 5 to get the number of workdays in those weeks, then work out the number of workdays between now and the start of those full weeks