本帖最后由 zmolli775 于 2019-11-26 08:58 编辑
近日工作需要·节假日API调用·需要判断那天是·节假日·休息日·工作日,网上找的免费API要嘛调用有次数限制·要嘛就是各种注册收费。所以自己撸了一个!
国家法定节假日 = 3
周六、周日 = 2
上班 = 1
错误 = 0
后期维护·对应修改紫色框日期即可。
php小白·欢迎各位大佬·纠错指正`以便完善此代码...
------------------------------------------------
好用加分...
调用方法get
http://你的域名/index.php/控制器名/(方法名)JJR?d=20190103(参数)
---------------------------------------------------------
更新2020年C#类
[C#] 纯文本查看 复制代码
/// <summary>
/// 节假日检测类
/// </summary>
public class JJR
{
public List<string[]>
// 2019年法定节假日
y2019 = new List<string[]>(),
// 2019年调休工作日
w2019 = new List<string[]>(),
// 2020年法定节假日
y2020 = new List<string[]>(),
// 2020年调休工作日
w2020 = new List<string[]>();
/// <summary>
/// 节假日检测(仅支持判断2019年、2020年)
/// </summary>
public JJR()
{
/*2019年有效数据*/
y2019.Add(new string[] { "20181230", "20190101" }); // 元旦
y2019.Add(new string[] { "20190204", "20190210" }); // 春节
y2019.Add(new string[] { "20190405", "20190407" }); // 清明
y2019.Add(new string[] { "20190501", "20190504" }); // 劳动
y2019.Add(new string[] { "20190607", "20190609" }); // 端午
y2019.Add(new string[] { "20190913", "20190915" }); // 中秋
y2019.Add(new string[] { "20191001", "20191007" }); // 国庆
// 调整工作日
w2019.Add(new string[] { "20181229", "20190202", "20190203", "20190428", "20190505", "20190929", "20191012" });
/*2020年有效数据*/
y2020.Add(new string[] { "20200101", "20200101" }); // 元旦
y2020.Add(new string[] { "20200124", "20200130" }); // 春节
y2020.Add(new string[] { "20200404", "20200406" }); // 清明
y2020.Add(new string[] { "20200501", "20200505" }); // 劳动
y2020.Add(new string[] { "20200625", "20200627" }); // 端午
y2020.Add(new string[] { "20201001", "20201008" }); // 中秋
y2020.Add(new string[] { "20201001", "20201008" }); // 国庆
// 调整工作日
w2020.Add(new string[] { "20200119", "20200201", "20200426", "20200509", "20200628", "20200927", "20201010" });
}
/// <summary>
/// 日期计算临时变量
/// </summary>
private DateTime temp;
/// <summary>
/// 检测日期状态(3:节假日、2:休息日、1:工作日、0:错误)
/// </summary>
/// <param name="rq">日期(20191231)</param>
/// <returns>返回状态码(3:节假日、2:休息日、1:工作日、0:错误)</returns>
public int CheckForHolidays(string rq)
{
temp = Str2dt(rq);
if ((temp.Year == 2019 || temp.Year == 2020) && temp != new DateTime())
{
return Determine();
}
else
{
/*Console.WriteLine("Error");*/
return 0;
}
}
/// <summary>
/// 检查法定节假日和调休工作日
/// </summary>
/// <param name="y">法定节假日</param>
/// <param name="w">调休工作日</param>
/// <returns>返回状态码(3:节假日、1:工作日、0:错误)</returns>
private int Check(List<string[]> y, List<string[]> w)
{
// 法定节假日检查
foreach (var i in y)
{
if (temp >= Str2dt(i[0]) && temp <= Str2dt(i[1]))
{
return 3;
}
}
// 调休工作日检查
foreach (string[] i in w)
{
foreach (var item in i)
{
if (temp == Str2dt(item))
{
return 1;
}
}
}
return 0;
}
private int Determine()
{
int type = 0;
switch (temp.Year)
{
// 2019年
case 2019:
type = Check(y2019, w2019); break;
// 2020年
case 2020:
type = Check(y2020, w2020); break;
}
if (type == 0)
{
int week = Convert.ToInt32(temp.DayOfWeek);
if (6 == week || 0 == week) // 类型为0`检测是否为星期六、星期日
{
type = 2; // 休息日
}
else
{
type = 1; // 工作日
}
}
/*
switch (type)
{
case 3: Console.WriteLine("节假日"); break;
case 2: Console.WriteLine("休息日"); break;
case 1: Console.WriteLine("工作日"); break;
}
*/
return type;
}
/// <summary>
/// string转换DateTime
/// </summary>
public static DateTime Str2dt(string d)
{
try
{
return DateTime.ParseExact(d, "yyyyMMdd", CultureInfo.CreateSpecificCulture("zh-CN"));
}
catch
{
return new DateTime();
}
} } |