C#如何基于输入工时计算排除周末及节假日的任务结束日期
C#计算排除周末与法定节假日的任务结束日期实现方案
核心逻辑
- 先计算所需总工作日:总工时除以每日工作时长,结果向上取整得到需要的完整工作日数量
- 从起始日期开始逐天校验,跳过周末、法定节假日,累计有效工作日,直到累计数达到所需总工作日,最终得到的日期即为任务结束日期
- 注意:法定节假日无系统内置数据,需自行维护集合配置,同时可额外配置调休上班的周末日期适配国内休假规则
完整实现代码
以下为在你原有输入逻辑基础上补充的完整可运行代码:
using System; using System.Collections.Generic; namespace WorkDateCalculator { class Program { // 配置法定节假日集合,可根据实际需求修改/从外部配置读取 private static readonly HashSet<DateTime> _holidays = new HashSet<DateTime> { // 示例:2021年元旦假期,可按实际年份补充 new DateTime(2021,1,1), new DateTime(2021,2,11),new DateTime(2021,2,12),new DateTime(2021,2,13), new DateTime(2021,2,14),new DateTime(2021,2,15),new DateTime(2021,2,16),new DateTime(2021,2,17) }; // 配置调休上班的周末集合,可根据实际需求修改/从外部配置读取 private static readonly HashSet<DateTime> _workWeekends = new HashSet<DateTime> { // 示例:2021年春节调休 new DateTime(2021,2,7),new DateTime(2021,2,20) }; static void Main(string[] args) { int totalHours = 0; int workingHoursPerDay = 0; DateTime startDate = DateTime.Now; bool allInputValid = true; // 读取总工时 Console.Write("输入总工时: "); if (Int32.TryParse(Console.ReadLine(), out int hours)) { if (hours <= 0) { Console.WriteLine("总工时不能为0或负数"); allInputValid = false; } else { totalHours = hours; Console.WriteLine("输入的总工时为: " + totalHours); } } else { Console.WriteLine("输入值不合法"); allInputValid = false; } if(!allInputValid) { Console.ReadLine(); return; } Console.ReadLine(); // 读取每日工作时长 Console.Write("输入每日工作时长: "); if (Int32.TryParse(Console.ReadLine(), out int workHours)) { if (workHours <= 0 || workHours > 9) { Console.WriteLine("每日工作时长最大为9小时,不能为0或负数"); allInputValid = false; } else { workingHoursPerDay = workHours; Console.WriteLine("输入的每日工作时长为: " + workingHoursPerDay); } } else { Console.WriteLine("输入值不合法"); allInputValid = false; } if(!allInputValid) { Console.ReadLine(); return; } Console.ReadLine(); // 读取开始日期 Console.WriteLine("输入开始日期 (格式示例 dd/mm/yyyy): "); while (!DateTime.TryParse(Console.ReadLine(), out startDate)) { Console.WriteLine("输入值不合法"); Console.WriteLine("输入开始日期 (格式示例 dd/mm/yyyy): "); } Console.WriteLine("输入的开始日期为: " + startDate.ToString("dd/MM/yyyy")); Console.ReadLine(); // 计算结束日期 int requiredWorkDays = (int)Math.Ceiling((double)totalHours / workingHoursPerDay); DateTime endDate = CalculateEndDate(startDate, requiredWorkDays); Console.WriteLine($"任务结束日期为:{endDate:dd/MM/yyyy}"); Console.ReadLine(); } /// <summary> /// 判断指定日期是否为有效工作日 /// </summary> private static bool IsWorkDay(DateTime date) { // 调休的周末直接算工作日 if (_workWeekends.Contains(date.Date)) return true; // 普通周末不算工作日 if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) return false; // 法定节假日不算工作日 if (_holidays.Contains(date.Date)) return false; return true; } /// <summary> /// 计算任务结束日期 /// </summary> private static DateTime CalculateEndDate(DateTime startDate, int requiredWorkDays) { DateTime currentDate = startDate.Date; int countedDays = 0; while (countedDays < requiredWorkDays) { if (IsWorkDay(currentDate)) { countedDays++; if (countedDays == requiredWorkDays) break; } currentDate = currentDate.AddDays(1); } return currentDate; } } }
示例运行结果
按照你给出的输入参数:总工时48小时、每日工作9小时、开始日期26/02/2021,不额外配置该时段节假日的情况下,计算得到结束日期为05/03/2021。
内容的提问来源于stack exchange,提问作者Jandi
相关产品推荐
相关产品推荐

