C#获取两个时间的时间差并去除周末(取工作日)的方法
作者:niuniu 时间:2022-07-25 00:20:44
本文实例讲述了C#获取两个时间的时间差并去除周末的方法。分享给大家供大家参考。具体分析如下:
一般来说取时间差的代码很多,但是能够只取工作日的时间差的代码很少,这段代码就来实现这一功能。
protected void Page_Load(object sender, EventArgs e)
{
DateTime start = Convert.ToDateTime("2012-12-10");
DateTime end= Convert.ToDateTime("2012-12-18");
TimeSpan span = end - start;
//int totleDay=span.Days;
//DateTime spanNu = DateTime.Now.Subtract(span);
int AllDays=Convert.ToInt32(span.TotalDays)+1;//差距的所有天数
int totleWeek = AllDays / 7;//差别多少周
int yuDay = AllDays % 7; //除了整个星期的天数
int lastDay = 0;
if (yuDay == 0) //正好整个周
{
lastDay = AllDays - (totleWeek * 2);
}
else
{
int weekDay = 0;
int endWeekDay = 0; //多余的天数有几天是周六或者周日
switch (start.DayOfWeek)
{
case DayOfWeek.Monday:
weekDay = 1;
break;
case DayOfWeek.Tuesday:
weekDay = 2;
break;
case DayOfWeek.Wednesday:
weekDay = 3;
break;
case DayOfWeek.Thursday:
weekDay = 4;
break;
case DayOfWeek.Friday:
weekDay = 5;
break;
case DayOfWeek.Saturday:
weekDay = 6;
break;
case DayOfWeek.Sunday:
weekDay = 7;
break;
}
if ((weekDay == 6 && yuDay >= 2) || (weekDay == 7 && yuDay >= 1) || (weekDay == 5 && yuDay >= 3) || (weekDay == 4 && yuDay >= 4) || (weekDay == 3 && yuDay >= 5) || (weekDay == 2 && yuDay >= 6) || (weekDay == 1 && yuDay >=7))
{
endWeekDay =2;
}
if ((weekDay == 6 && yuDay < 1) || (weekDay == 7 && yuDay <5) || (weekDay == 5 && yuDay < 2) || (weekDay == 4 && yuDay < 3) || (weekDay == 3 && yuDay < 4) || (weekDay == 2 && yuDay < 5) || (weekDay == 1 && yuDay < 6)) {
endWeekDay = 1;
}
lastDay = AllDays - (totleWeek * 2) - endWeekDay;
}
lblTime.Text = lastDay.ToString();
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,时间差
0
投稿
猜你喜欢
Android图像处理之霓虹滤镜效果
2023-08-11 14:37:41
详解Java如何实现在PDF中插入,替换或删除图像
2022-04-07 22:40:36
Android进阶事件分发机制解决事件冲突
2021-08-22 06:12:34
SpringBoot自动装配之Condition深入讲解
2023-12-03 02:20:29
Android时间对话框TimePickerDialog详解
2022-08-23 05:50:06
MyBatis学习教程(四)-如何快速解决字段名与实体类属性名不相同的冲突问题
2023-11-25 05:43:49
C#实现六大设计原则之依赖倒置原则
2023-06-27 04:02:57
Java 中普通代码块,构造代码块,静态代码块区别及代码示例
2022-07-03 03:54:21
Android简单实现启动画面的方法
2022-08-25 17:54:55
Android使用IntentService进行apk更新示例代码
2022-01-19 12:12:07
Java内存模型JMM与volatile
2022-03-25 09:33:50
Java算法设计与分析分治算法
2022-04-02 08:07:15
C++ 异常处理 catch(...)介绍
2023-06-24 05:35:32
简单理解java泛型的本质(非类型擦除)
2023-10-13 03:54:34
SpringBoot SpEL语法扫盲与查询手册的实现
2022-06-02 23:52:39
C#新手常犯的错误汇总
2021-10-29 05:05:53
java中Path和ClassPath用法比较
2022-03-15 03:41:26
java 中的instanceof用法详解及instanceof是什么意思(推荐)
2023-06-07 13:52:27
C#实现过滤sql特殊字符的方法集合
2022-01-30 23:58:04
Spring Security中使用authorizeRequests遇到的问题小结
2023-10-07 04:23:45