计算俩个日期之间的工作日(用C#)
根据公司项目的需求要计算俩个日期之间的工作日天数,写了一个方法来处理,如下
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication1
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 DateTime dt1 = new DateTime(2007, 06, 14);
12 DateTime dt2 = DateTime.Now;
13
14 TimeSpan ts1 = dt2.Subtract(dt1);
15 int countday = ts1.Days;//自然天数
16 int weekday = 0;//工作日
17 for (int i = 0; i < countday; i++)
18 {
19 DateTime tempdt = dt1.Date.AddDays(i);
20 if (tempdt.DayOfWeek != System.DayOfWeek.Saturday && tempdt.DayOfWeek != System.DayOfWeek.Sunday)
21 {
22 weekday++;
23 }
24 }
25 Console.WriteLine("自然天数"+countday.ToString());
26 Console.WriteLine("工作日" + weekday.ToString());
27 Console.ReadLine();
28 }
29 }
30}
31
2using System.Collections.Generic;
3using System.Text;
4
5namespace ConsoleApplication1
6{
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 DateTime dt1 = new DateTime(2007, 06, 14);
12 DateTime dt2 = DateTime.Now;
13
14 TimeSpan ts1 = dt2.Subtract(dt1);
15 int countday = ts1.Days;//自然天数
16 int weekday = 0;//工作日
17 for (int i = 0; i < countday; i++)
18 {
19 DateTime tempdt = dt1.Date.AddDays(i);
20 if (tempdt.DayOfWeek != System.DayOfWeek.Saturday && tempdt.DayOfWeek != System.DayOfWeek.Sunday)
21 {
22 weekday++;
23 }
24 }
25 Console.WriteLine("自然天数"+countday.ToString());
26 Console.WriteLine("工作日" + weekday.ToString());
27 Console.ReadLine();
28 }
29 }
30}
31