C#判断指定时间是否为法定节假日
最近在做账单功能,需要智能跳过节假日生成账单。在网上搜了一下,发现目前现成的接口都是需要购买的,而且价格还是比较贵的。本着求人不如求己的原则,自己整理了一下2021年的节假日跟周末补班的日期,写了一个小的demo,有需要的可以自取。
使用注意:
specalWeek2021和holiday2021需要根据每年的法定节假日进行更改,否则将导致判断不准确。
核心代码:
/// <summary>
/// 判断法定工作日
/// </summary>
/// <param name="dt">时间</param>
/// <returns>0上班,1不需要上班的周末(也包括节假日),2法定节假日</returns>
public int IsHolidayOrWeeksu(DateTime dt)
{
//特殊的周末(周末却上班)
string[] specalWeek2021 = { "0207", "0213", "0214", "0220", "0425", "0508", "0918", "0926", "1009" };
//法定假日
string[] holiday2021 = { "0101", "0211", "0212","0215","0216","0217","0403","0404","0405","0501","0502","0503","0504","0505","0614","0920","0921","1001","1004","1005","1006","1007" };
bool isHoildayOrWeek = false;
//取年
string weekYear = dt.Year.ToString();
//取月日
string[] weekDate = { dt.ToString("MMdd") };
//判断周末
if ((int)dt.DayOfWeek == 0 || (int)dt.DayOfWeek == 6)
{
//周末是否需要上班
switch (weekYear)
{
case "2021":
isHoildayOrWeek = specalWeek2021.Intersect(weekDate).Count() == 0;
break;
}
}
if (isHoildayOrWeek)
{
return 1;
}
//判断法定节假日
switch (weekYear)
{
case "2021":
isHoildayOrWeek = holiday2021.Intersect(weekDate).Count() > 0;
break;
}
if (isHoildayOrWeek)
{
return 2;
}
return 0;
}
文章配图:
版权声明:
作者:兴兴
文章:C#判断指定时间是否为法定节假日
链接:https://www.networkcabin.com/notes/2609
文章版权归本站所有,未经授权请勿转载。
作者:兴兴
文章:C#判断指定时间是否为法定节假日
链接:https://www.networkcabin.com/notes/2609
文章版权归本站所有,未经授权请勿转载。
THE END