C#跳过工作日,计算几个工作日之后到期的方法
需求:消费者投诉企业,企业在2个工作日之内做出应答。
分析:1.工作日要刨去周末和法定节假日,而且每年的节假日不一样。
2.消费者可以在任意时间发起投诉,如果在非工作日发起了投诉,那么计算时间应该从工作日开始计算,就要刨去当日的时和分。
以下是代码:
public class HolidayHelper { /// <summary> /// 判断法定工作日 /// </summary> /// <param name="dt">时间</param> /// <returns>0上班,1不需要上班的周末(也包括节假日),2法定节假日</returns> public static int IsHolidayOrWeek(DateTime dt) { //特殊的周末(周末却上班) string[] specalWeek2009 = { "0104", "0124", "0201", "0531", "0927", "1010" }; string[] specalWeek2010 = { "0220", "0221", "0612", "0613", "0919", "0925", "0926", "1009" }; string[] specalWeek2011 = { "0130", "0212", "0402", "1008", "1009" }; string[] specalWeek2012 = { "0129", "0331", "0401", "0428", "0929" }; string[] specalWeek2013 = { "0105", "0106", "0216", "0217", "0407", "0427", "0428", "0608", "0609", "0922", "0929", "1012" }; string[] specalWeek2014 = { "0126", "0208", "0504", "0928", "1011" }; string[] specalWeek2015 = { "0104", "0215", "0228", "0906", "1010" }; string[] specalWeek2016 = { "0206", "0214", "0612", "0918", "1008", "1009" }; string[] specalWeek2017 = { "0122", "0204", "0401", "0527", "0930" }; //法定假日 string[] holiday2009 = { "0101", "0102", "0103", "0125", "0126", "0127", "0128", "0129", "0130", "0131", "0404", "0405", "0406", "0501", "0502", "0503", "0528", "0529", "0530", "1001", "1002", "1003", "1004", "1005", "1006", "1007", "1008", }; string[] holiday2010 = { "0101", "0102", "0103", "0213", "0214", "0215", "0216", "0217", "0218", "0219", "0403", "0404", "0405", "0501", "0502", "0503", "0614", "0615", "0616", "0922", "0923", "0924", "1001", "1002", "1003", "1004", "1005", "1006", "1007" }; string[] holiday2011 = { "0101", "0102", "0103", "0202", "0203", "0204", "0205", "0206", "0207", "0208", "0403", "0404", "0405", "0430", "0501", "0502", "0604", "0605", "0606", "0910", "0911", "0912", "1001", "1002", "1003", "1004", "1005", "1006", "1007", }; string[] holiday2012 = { "0101", "0102", "0103", "0122", "0123", "0124", "0125", "0126", "0127", "0128", "0402", "0403", "0404", "0429", "0430", "0501", "0622", "0623", "0624", "0930", "1001", "1002", "1003", "1004", "1005", "1006", "1007", }; string[] holiday2013 = { "0101", "0102", "0103", "0209", "0210", "0211", "0212", "0213", "0214", "0215", "0404", "0405", "0406", "0429", "0430", "0501", "0610", "0611", "0612", "0919", "0920", "0921", "1001", "1002", "1003", "1004", "1005", "1006", "1007", }; string[] holiday2014 = { "0101", "0131", "0201", "0202", "0203", "0204", "0205", "0206", "0405", "0406", "0407", "0501", "0502", "0503", "0602", "0908", "1001", "1002", "1003", "1004", "1005", "1006", "1007", }; string[] holiday2015 = { "0101", "0102", "0103", "0218", "0219", "0220", "0221", "0222", "0223", "0224", "0404", "0405", "0406", "0501", "0502", "0503", "0620", "0621", "0622", "0903", "0904", "0905", "0927", "1001", "1002", "1003", "1004", "1005", "1006", "1007", }; string[] holiday2016 = { "0101", "0207", "0208", "0209", "0210", "0211", "0212", "0213", "0404", "0501", "0502", "0609", "0610", "0611", "0915", "0916", "0917", "1001", "1002", "1003", "1004", "1005", "1006", "1007", }; string[] holiday2017 = { "0101", "0102", "0127", "0128", "0129", "0130", "0131", "0201", "0202", "0403", "0404", "0501", "0529", "0530", "1001", "1002", "1003", "1004", "1005", "1006", }; 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 "2009": isHoildayOrWeek = specalWeek2009.Intersect(weekDate).Count() == 0; break; case "2010": isHoildayOrWeek = specalWeek2010.Intersect(weekDate).Count() == 0; break; case "2011": isHoildayOrWeek = specalWeek2011.Intersect(weekDate).Count() == 0; break; case "2012": isHoildayOrWeek = specalWeek2012.Intersect(weekDate).Count() == 0; break; case "2013": isHoildayOrWeek = specalWeek2013.Intersect(weekDate).Count() == 0; break; case "2014": isHoildayOrWeek = specalWeek2014.Intersect(weekDate).Count() == 0; break; case "2015": isHoildayOrWeek = specalWeek2015.Intersect(weekDate).Count() == 0; break; case "2016": isHoildayOrWeek = specalWeek2016.Intersect(weekDate).Count() == 0; break; case "2017": isHoildayOrWeek = specalWeek2017.Intersect(weekDate).Count() == 0; break; } } if (isHoildayOrWeek) { return 1; } //判断法定节假日 switch (weekYear) { case "2009": isHoildayOrWeek = holiday2009.Intersect(weekDate).Count() > 0; break; case "2010": isHoildayOrWeek = holiday2010.Intersect(weekDate).Count() > 0; break; case "2011": isHoildayOrWeek = holiday2011.Intersect(weekDate).Count() > 0; break; case "2012": isHoildayOrWeek = holiday2012.Intersect(weekDate).Count() > 0; break; case "2013": isHoildayOrWeek = holiday2013.Intersect(weekDate).Count() > 0; break; case "2014": isHoildayOrWeek = holiday2014.Intersect(weekDate).Count() > 0; break; case "2015": isHoildayOrWeek = holiday2015.Intersect(weekDate).Count() > 0; break; case "2016": isHoildayOrWeek = holiday2016.Intersect(weekDate).Count() > 0; break; case "2017": isHoildayOrWeek = holiday2017.Intersect(weekDate).Count() > 0; break; } if (isHoildayOrWeek) { return 2; } return 0; } /// <summary> /// 工作日时间差 /// </summary> /// <param name="days">几个工作日后</param> /// <returns>延迟毫秒数</returns> public static double GetDelpyTime(int days) { int sign = 0; for (int i = 0; i < days; i++) { if (i == 0 && IsHolidayOrWeek(DateTime.Now.AddDays(i)) > 0) { //非工作日发起的投诉,要抹掉小时和分钟 sign = 1; } if (IsHolidayOrWeek(DateTime.Now.AddDays(i)) > 0) { days = days + 1; }; } TimeSpan ts = new TimeSpan(); if (sign == 1) { ts = (DateTime.Now.AddDays(days) - DateTime.Now.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute)); } else { ts = DateTime.Now.AddDays(days) - DateTime.Now; } return ts.TotalMilliseconds; } }
C#跳过工作日,计算几个工作日之后到期的方法的更多相关文章
- Flink实时计算pv、uv的几种方法
本文首发于:Java大数据与数据仓库,Flink实时计算pv.uv的几种方法 实时统计pv.uv是再常见不过的大数据统计需求了,前面出过一篇SparkStreaming实时统计pv,uv的案例,这里用 ...
- C++11用于计算函数对象返回类型的统一方法
[C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...
- Java日期时间API系列38-----一种高效的工作日计算计算方法
如果没有节日放假调休的话,工作日很好计算,周一到周五就是工作日,但因为有节日放假调休,使得这个计算需要外部放假安排数据来支持.计算原理: 先按照放假安排数据计算,再按照周一周五计算. 下面以Local ...
- PostgreSQL 当月最后一天的工作日 , 计算日期是星期几
可以用pg自带函数select extract(dow from current_date),之所以没用主要是展示一下通过数学方法计算日期的原理. drop function if exists ge ...
- Servlet3.0 jsp跳转到Servlet 出现404错误的路径设置方法
最近又遇到了这种问题,百度了好久,发现有人说要在action的路径里面写Servlet文件的绝对路径,比如说,单独打开servlet的地址为http://localhost:8080/TomcatTe ...
- form表单action提交表单,页面不跳转且表单数据含文件的处理方法
在最近的项目中需要将含 input[type='file']的表单提交给后台 ,并且后台需要将文件存储在数据库中.之前所用的方法都是先将文件上传到七牛服务器上,然后七牛会返回文件的下载地址,在提交表单 ...
- iOS从App跳转至系统设置菜单各功能项的编写方法讲解
跳到系统设置里的WiFi界面 info里面设置: 在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图 代码: 复制代码 代码如下: NSUR ...
- 用Regex类计算一个字符串出现次数是最好方法【转载】
我的一个朋友问我,怎么在c#或vb.net中,计算一个字符串中查找另一个字符串中出现的次数,他说在网上打了好多方法,我看了一下,有的是用replace的方法去实现,这种方法不是太好,占资源太大了.其实 ...
- 实现js浮点数加、减、乘、除的精确计算(网上很多文章里的方法是不能解决所有js浮点数计算误差的)
最近做项目,要用到js的加.减.乘.除的计算,发现js浮点数计算会有一些误差. 网上有很多文章都有js浮点数计算误差的解决方法,说能解决这个问题,But…….比如一个加法函数,如下: function ...
随机推荐
- C/C++编程日记:逻辑井字棋(圈叉)(用空格初始化)
问题描述: 3*3的棋盘中,只要一条线上出现三个一样的棋子就获胜(玩家或电脑):如果棋盘已经放满还未出现三个棋子一条线则打成平手. 具体细节: 初始化棋盘(用空格初始化) //初始化棋盘 vo ...
- 利用DNS实现SQL注入带外查询(OOB)
根据用于数据检索的传输信道,SQLi可分为三个独立的类别:inference(经典SQL注入),inband(盲注.推理注入.带内注入)和out-of-band 一.什么是OOB out-of-ban ...
- 面试半年,凭借这份JVM面试题,我终于拿到了字节跳动的offer!
内存区域 虚拟机栈生命周期与线程相同,描述的是Java 方法执行的内存模型,每个方法在执行的时候都会创建一个栈帧,用于存取局部变量表.操作数栈.动态链接.方法出口等信息本地方法栈与虚拟机栈作用相似,只 ...
- jenkins运行错误解决办法
本地搭建好jenkis服务器,运行然后登陆上管理后台 构建好运行命令,然后进行构建出现如下错误说没有python运行环境 ,本地已经配置好python环境,并且终端能正常运行. 解决方法:jenkin ...
- C语言是如何诞生的?地位怎样?未来发展趋势?
C语言的历史 C语言的原型是A语言(ALGOL 60语言). 1963年,剑桥大学将ALGOL 60语言发展成为CPL(Combined Programming Language)语言. 1967 ...
- vs2019 Com组件初探-简单的COM编写以及实现跨语言调用
前提条件 1.掌握C++基础语法 2.平台安装 vs2019 3.本地平台为 windows 10 1909 X64 4.了解vbs基础语法 本次目标 1.掌握Com组件的概念及原理 2.编写一个简单 ...
- 微信小程序 下拉刷新
<scroll-view class='scroll-view-container' scroll-y="true" bindscrolltolower='scrollToL ...
- 「考试」CSP-S 2020
乱扯 爆炸的过程是这样的 写了\(2.5h\)的\(T1\)过不去大样例,自闭了 决定调\(T2\)然后过了样例但事实上写的完全是假的 这个时候突然\(T1\)灵光一闪就没再看\(T2\)了 然后就一 ...
- django项目初始化
1.为了方便管理app,我们添加专门的apps文件夹来存放所有的app.结构如下 1.1设置完apps文件夹以后我们需要对配置文件做相应的更改 1.1.1.在seetings.py里添加django文 ...
- 微软面试题: LeetCode 151. 翻转字符串里的单词 出现次数:6
题目描述: 给定一个字符串,逐个翻转字符串中的每个单词. 说明: 无空格字符构成一个 单词 .输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括.如果两个单词间有多余的空格,将反转后 ...