C#获取每年节假日

备注:

TYPE 类型(正常工作日:0, 法定节假日:1, 节假日调休补班:2,休息日: 3)

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

using System;

using System.Collections.Generic;

using System.Data;

using System.IO;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading.Tasks;

namespace Test.Common

{

//节假日

public class Holiday

{

public void main(string yearNow)

{

string[] arrTear = new string[] { yearNow };

for (int i = 0; i < arrTear.Length; i++)

{

DataTable table = new DataTable();

table.Columns.Add("ShortDay");

table.Columns.Add("Code");

string year = arrTear[i];

string startDay = year + "-01-01";

string endDay = year + "-12-31";

//获取相差几天

int days = DateDiff(Convert.ToDateTime(startDay), Convert.ToDateTime(endDay)) + 1;

//添加整一年的数据并区分出工作日与休息日

for (int j = 0; j < days; j++)

{

DataRow dr = table.NewRow();

dr["ShortDay"] = Convert.ToDateTime(startDay).AddDays(j).ToString("yyyyMMdd");

//判断是否未工作日

string temp = GetWeekDes((int)Convert.ToDateTime(startDay).AddDays(j).DayOfWeek);

if (temp == "星期日" || temp == "星期六")

{

dr["Code"] = 3;

}

else

{

dr["Code"] = 0;

}

table.Rows.Add(dr);

}

//获取改年的节假日信息

string result = "";

try

{

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://timor.tech/api/holiday/year/" + year + "/");

req.Method = "GET";

req.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

Stream streamNr = resp.GetResponseStream();

//获取响应内容

using (StreamReader reader = new StreamReader(streamNr, Encoding.UTF8))

{

result = reader.ReadToEnd();

}

}

catch (Exception e)

{

}

if (result != "")

{

JObject jo = (JObject)JsonConvert.DeserializeObject(result);

JObject jo_T = (JObject)JsonConvert.DeserializeObject(jo["holiday"].ToString());

foreach (var item in jo_T)

{

string key = item.Key;

JObject jo_T2 = (JObject)JsonConvert.DeserializeObject(item.Value.ToString());

string holiday = jo_T2["holiday"].ToString();

string dateT = jo_T2["date"].ToString();

string date = Convert.ToDateTime(dateT).ToString("yyyyMMdd");

for (int k = 0; k < table.Rows.Count; k++)

{

if (table.Rows[k]["ShortDay"].ToString() == date)

{

//法定节假日

if (holiday == "True" || holiday == "true")

{

table.Rows[k]["Code"] = 1;

}

//节假日调休补班

else

{

table.Rows[k]["Code"] = 2;

}

break;

}

}

}

}

string txt = "";

//添加记录

for (int j = 0; j < table.Rows.Count; j++)

{

string sql = "INSERT INTO HOLIDAY(SHORT_DATE,TYPE) values('{0}','{1}');";

sql = string.Format(sql, table.Rows[j]["ShortDay"], table.Rows[j]["Code"]);

txt += sql;

}

}

}

/// <summary>

/// 获取日期天数差

/// </summary>

/// <param name="dateStart"></param>

/// <param name="dateEnd"></param>

/// <returns></returns>

private int DateDiff(DateTime dateStart, DateTime dateEnd)

{

DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());

DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());

TimeSpan sp = end.Subtract(start);

return sp.Days;

}

private string GetWeekDes(int index)

{

switch (index)

{

case 0: return "星期日";

case 1: return "星期一";

case 2: return "星期二";

case 3: return "星期三";

case 4: return "星期四";

case 5: return "星期五";

case 6: return "星期六";

}

return "";

}

}

}

相关推荐
合作小小程序员小小店42 分钟前
桌面开发,食堂卡管理系统开发,基于C#,winform,mysql数据库
数据库·mysql·c#
努力的ping43 分钟前
qt信号和槽
开发语言·qt
AGANCUDA44 分钟前
qt中vtk显示pcl的点云类
开发语言·qt
小马过河R44 分钟前
tRPC-GO 框架Helloworld实践初体验
开发语言·分布式·后端·架构·golang·gin·beego
GoodStudyAndDayDayUp1 小时前
WIN11安装配置验证java\maven
java·开发语言·maven
fish_xk1 小时前
用c++写控制台贪吃蛇
开发语言·c++
王燕龙(大卫)1 小时前
rust:trait
开发语言·后端·rust
合作小小程序员小小店1 小时前
桌面开发,物业管理系统开发,基于C#,winform,mysql数据库
开发语言·数据库·sql·mysql·microsoft·c#
程序员-周李斌1 小时前
LinkedList 源码深度分析(基于 JDK 8)
java·开发语言·数据结构·list