C# 根据年、月、周、星期获得日期等
时间:2011-03-17 来源:wulg
1 /// <summary>
2 /// 取指定日期是一年中的第几周
3 /// </summary>
4 /// <param name="dtime">给定的日期</param>
5 /// <returns>数字 一年中的第几周</returns>
6 public static int WeekOfYear(DateTime dtime)
7 {
8 try
9 {
10 //确定此时间在一年中的位置
11 int dayOfYear = dtime.DayOfYear;
12 //当年第一天
13 DateTime tempDate = new DateTime(dtime.Year, 1, 1);
14 //确定当年第一天
15 int tempDayOfWeek = (int)tempDate.DayOfWeek;
16 tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
17 ////确定星期几
18 int index = (int)dtime.DayOfWeek;
19 index = index == 0 ? 7 : index;
20
21 //当前周的范围
22 DateTime retStartDay = dtime.AddDays(-(index - 1));
23 DateTime retEndDay = dtime.AddDays(6 - index);
24
25 //确定当前是第几周
26 int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
27
28 if (retStartDay.Year < retEndDay.Year)
29 {
30 weekIndex = 1;
31 }
32
33 return weekIndex;
34 }
35 catch (Exception ex)
36 {
37 throw new Exception(ex.Message);
38 }
39
40 }
41
42 /// <summary>
43 /// 求某年有多少周
44 /// </summary>
45 /// <param name="dtime"></param>
46 /// <returns></returns>
47 public static int GetWeekOfYear(DateTime dtime)
48 {
49 int countDay = DateTime.Parse(dtime.Year + "-12-31").DayOfYear;
50 int countWeek = countDay / 7;
51 return countWeek;
52
59 }
60
61
62 //根据年月日获得星期几
63 public static string CaculateWeekDay(int month, int day)
64 {
65 string weekstr = string.Empty;
66 int year = DateTime.Now.Year;
67 //把一月和二月看成是上一年的十三月和十四月
68 if (month == 1) { month = 13; year--; }
69 if (month == 2) { month = 14; year--; }
70 int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
71 switch (week)
72 {
73 case 0: weekstr = "1"; break;
74 case 1: weekstr = "2"; break;
75 case 2: weekstr = "3"; break;
76 case 3: weekstr = "4"; break;
77 case 4: weekstr = "5"; break;
78 case 5: weekstr = "6"; break;
79 case 6: weekstr = "7"; break;
80 }
81 return weekstr;
82 }
83
84 /// <summary>
85 /// 根据2个时间段获得相应的周数
86 /// </summary>
87 /// <param name="beginDate">开始时间</param>
88 /// <param name="endDate">结束时间</param>
89 /// <returns></returns>
90 public static int WeekOfDate(DateTime beginDate, DateTime endDate)
91 {
92 TimeSpan ts1 = new TimeSpan(beginDate.Ticks);
93 TimeSpan ts2 = new TimeSpan(endDate.Ticks);
94 TimeSpan ts = ts2.Subtract(ts1).Duration();
95 int weeks = ts.Days / 7;
96
97 //确定此时间在一年中的位置
98 int dayOfYear = beginDate.DayOfYear;
99 //当年第一天
100 DateTime tempDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
101 //最后一天
102 DateTime tempendDate = new DateTime(endDate.Year, endDate.Month, endDate.Day);
103 int tempDayOfWeek = (int)tempDate.DayOfWeek;
104 tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
105
106 ////确定星期几
107 int startindex = (int)beginDate.DayOfWeek;
108 startindex = startindex == 0 ? 7 : startindex;
109 //当前周的范围
110 DateTime retStartDay = beginDate.AddDays(-(startindex - 1));
111 DateTime retEndDay = beginDate.AddDays(7 - startindex);
112 //确定当前是第几周
113 int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
114
115 return weeks;
116 }
117
118 /// <summary>
119 /// 根据起始时间,获取第几周
120 /// </summary>
121 /// <param name="dtime">当前时间</param>
122 /// <returns></returns>
123 public static int WeekOfTermDate(DateTime dtime)
124 {
125 string datetime = "2011-3-1";
126 TimeSpan ts1 = new TimeSpan(dtime.Ticks);
127 TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime[0]).Ticks);
128 TimeSpan ts = ts2.Subtract(ts1).Duration();
129
130 //确定此时间在一年中的位置
131 int dayOfYear = ts.Days;
132 //当年第一天
133 DateTime tempDate = new DateTime(Convert.ToDateTime(datetime[0]).Year, Convert.ToDateTime(datetime[0]).Month, Convert.ToDateTime(datetime[0]).Day);
134
135 int tempDayOfWeek = (int)tempDate.DayOfWeek;
136 tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
137 ////确定星期几
138 int index = (int)dtime.DayOfWeek;
139 index = index == 0 ? 7 : index;
140
141 //当前周的范围
142 DateTime retStartDay = dtime.AddDays(-(index - 1));
143 DateTime retEndDay = dtime.AddDays(7 - index);
144
145 //确定当前是第几周
146 int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek) / 7);
147
148 return weekIndex;
149 }
150
151 /// <summary>
152 /// 根据学期,周,星期获得具体年月日
153 /// </summary>
154 /// <param name="term">开始时间</param>
155 /// <param name="week">第几周</param>
156 /// <param name="day">星期几</param>
157 /// <returns></returns>
158 public static DateTime DateTimeByWeekAndDay(string year,int week, int day)
159 {
160 DateTime someTime = Convert.ToDateTime(year);
161
162 int i = someTime.DayOfWeek - DayOfWeek.Monday;
163 if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
164 TimeSpan ts = new TimeSpan(i, 0, 0, 0);
165
166 //获取第N周的星期一
167 someTime = someTime.Subtract(ts).AddDays((week - 1) * 7);
168 //获得星期几
169 someTime = someTime.AddDays(day - 1);
170 return someTime;
171 }
2 /// 取指定日期是一年中的第几周
3 /// </summary>
4 /// <param name="dtime">给定的日期</param>
5 /// <returns>数字 一年中的第几周</returns>
6 public static int WeekOfYear(DateTime dtime)
7 {
8 try
9 {
10 //确定此时间在一年中的位置
11 int dayOfYear = dtime.DayOfYear;
12 //当年第一天
13 DateTime tempDate = new DateTime(dtime.Year, 1, 1);
14 //确定当年第一天
15 int tempDayOfWeek = (int)tempDate.DayOfWeek;
16 tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
17 ////确定星期几
18 int index = (int)dtime.DayOfWeek;
19 index = index == 0 ? 7 : index;
20
21 //当前周的范围
22 DateTime retStartDay = dtime.AddDays(-(index - 1));
23 DateTime retEndDay = dtime.AddDays(6 - index);
24
25 //确定当前是第几周
26 int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
27
28 if (retStartDay.Year < retEndDay.Year)
29 {
30 weekIndex = 1;
31 }
32
33 return weekIndex;
34 }
35 catch (Exception ex)
36 {
37 throw new Exception(ex.Message);
38 }
39
40 }
41
42 /// <summary>
43 /// 求某年有多少周
44 /// </summary>
45 /// <param name="dtime"></param>
46 /// <returns></returns>
47 public static int GetWeekOfYear(DateTime dtime)
48 {
49 int countDay = DateTime.Parse(dtime.Year + "-12-31").DayOfYear;
50 int countWeek = countDay / 7;
51 return countWeek;
52
59 }
60
61
62 //根据年月日获得星期几
63 public static string CaculateWeekDay(int month, int day)
64 {
65 string weekstr = string.Empty;
66 int year = DateTime.Now.Year;
67 //把一月和二月看成是上一年的十三月和十四月
68 if (month == 1) { month = 13; year--; }
69 if (month == 2) { month = 14; year--; }
70 int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
71 switch (week)
72 {
73 case 0: weekstr = "1"; break;
74 case 1: weekstr = "2"; break;
75 case 2: weekstr = "3"; break;
76 case 3: weekstr = "4"; break;
77 case 4: weekstr = "5"; break;
78 case 5: weekstr = "6"; break;
79 case 6: weekstr = "7"; break;
80 }
81 return weekstr;
82 }
83
84 /// <summary>
85 /// 根据2个时间段获得相应的周数
86 /// </summary>
87 /// <param name="beginDate">开始时间</param>
88 /// <param name="endDate">结束时间</param>
89 /// <returns></returns>
90 public static int WeekOfDate(DateTime beginDate, DateTime endDate)
91 {
92 TimeSpan ts1 = new TimeSpan(beginDate.Ticks);
93 TimeSpan ts2 = new TimeSpan(endDate.Ticks);
94 TimeSpan ts = ts2.Subtract(ts1).Duration();
95 int weeks = ts.Days / 7;
96
97 //确定此时间在一年中的位置
98 int dayOfYear = beginDate.DayOfYear;
99 //当年第一天
100 DateTime tempDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day);
101 //最后一天
102 DateTime tempendDate = new DateTime(endDate.Year, endDate.Month, endDate.Day);
103 int tempDayOfWeek = (int)tempDate.DayOfWeek;
104 tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
105
106 ////确定星期几
107 int startindex = (int)beginDate.DayOfWeek;
108 startindex = startindex == 0 ? 7 : startindex;
109 //当前周的范围
110 DateTime retStartDay = beginDate.AddDays(-(startindex - 1));
111 DateTime retEndDay = beginDate.AddDays(7 - startindex);
112 //确定当前是第几周
113 int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek - 1) / 7);
114
115 return weeks;
116 }
117
118 /// <summary>
119 /// 根据起始时间,获取第几周
120 /// </summary>
121 /// <param name="dtime">当前时间</param>
122 /// <returns></returns>
123 public static int WeekOfTermDate(DateTime dtime)
124 {
125 string datetime = "2011-3-1";
126 TimeSpan ts1 = new TimeSpan(dtime.Ticks);
127 TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime[0]).Ticks);
128 TimeSpan ts = ts2.Subtract(ts1).Duration();
129
130 //确定此时间在一年中的位置
131 int dayOfYear = ts.Days;
132 //当年第一天
133 DateTime tempDate = new DateTime(Convert.ToDateTime(datetime[0]).Year, Convert.ToDateTime(datetime[0]).Month, Convert.ToDateTime(datetime[0]).Day);
134
135 int tempDayOfWeek = (int)tempDate.DayOfWeek;
136 tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
137 ////确定星期几
138 int index = (int)dtime.DayOfWeek;
139 index = index == 0 ? 7 : index;
140
141 //当前周的范围
142 DateTime retStartDay = dtime.AddDays(-(index - 1));
143 DateTime retEndDay = dtime.AddDays(7 - index);
144
145 //确定当前是第几周
146 int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek) / 7);
147
148 return weekIndex;
149 }
150
151 /// <summary>
152 /// 根据学期,周,星期获得具体年月日
153 /// </summary>
154 /// <param name="term">开始时间</param>
155 /// <param name="week">第几周</param>
156 /// <param name="day">星期几</param>
157 /// <returns></returns>
158 public static DateTime DateTimeByWeekAndDay(string year,int week, int day)
159 {
160 DateTime someTime = Convert.ToDateTime(year);
161
162 int i = someTime.DayOfWeek - DayOfWeek.Monday;
163 if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。
164 TimeSpan ts = new TimeSpan(i, 0, 0, 0);
165
166 //获取第N周的星期一
167 someTime = someTime.Subtract(ts).AddDays((week - 1) * 7);
168 //获得星期几
169 someTime = someTime.AddDays(day - 1);
170 return someTime;
171 }
相关阅读 更多 +