some code about date.welcome correct!
Some day, in my project need to get the moth, weekly,quarter and others. So i write some code to meet that ,then i put them to my blog. Hopt to help somebody need it!
1public class AboutDate
2{
3 public AboutDate()
4 {
5 //
6 // TODO: Add constructor logic here
7 //
8 }
9
10 /// <summary>
11 /// 取得近10年的数据
12 /// </summary>
13 /// <param name="info">
14 /// 偏移量(info为n,则包含 当前年+n 的年份)
15 /// </param>
16 /// <returns>
17 /// DataSet
18 ///</returns>
19
20 public DataSet GetYearData(int info)
21 {
22 DataSet dataSet = new DataSet();
23 dataSet.Tables.Add();
24 dataSet.Tables[0].Columns.Add("YearText");
25 dataSet.Tables[0].Columns.Add("YearValue");
26
27 for (int i = 10; i >= 0; i--)
28 {
29 DataRow dataRow = dataSet.Tables[0].NewRow();
30 dataRow["YearText"] = DateTime.Now.Year - i + info;
31 dataRow["YearValue"] = DateTime.Now.Year - i + info;
32 dataSet.Tables[0].Rows.Add(dataRow);
33 }
34 return dataSet;
35 }
36
37 /// <summary>
38 /// 取得季度数据
39 /// </summary>
40 /// <returns>
41 /// DataSet
42 /// </returns>
43 public DataSet GetQuarterData()
44 {
45 DataSet dataSet = new DataSet();
46 dataSet.Tables.Add();
47 dataSet.Tables[0].Columns.Add("QuarterText");
48 dataSet.Tables[0].Columns.Add("QuarterValue");
49 DataRow dataRow1 = dataSet.Tables[0].NewRow();
50 DataRow dataRow2 = dataSet.Tables[0].NewRow();
51 DataRow dataRow3 = dataSet.Tables[0].NewRow();
52 DataRow dataRow4 = dataSet.Tables[0].NewRow();
53
54 dataRow1["QuarterText"] = "第一季度";
55 dataRow1["QuarterValue"] = "1";
56
57 dataRow2["QuarterText"] = "第二季度";
58 dataRow2["QuarterValue"] = "2";
59
60 dataRow3["QuarterText"] = "第三季度";
61 dataRow3["QuarterValue"] = "3";
62
63 dataRow4["QuarterText"] = "第四季度";
64 dataRow4["QuarterValue"] = "4";
65
66 dataSet.Tables[0].Rows.Add(dataRow1);
67 dataSet.Tables[0].Rows.Add(dataRow2);
68 dataSet.Tables[0].Rows.Add(dataRow3);
69 dataSet.Tables[0].Rows.Add(dataRow4);
70 return dataSet;
71 }
72
73 /// <summary>
74 /// 取得月份数据,每年12个月
75 /// </summary>
76 /// <returns>
77 /// DataSet
78 /// </returns>
79 public DataSet GetMonthData()
80 {
81 DataSet dataSet = new DataSet();
82 dataSet.Tables.Add();
83 dataSet.Tables[0].Columns.Add("MonthText");
84 dataSet.Tables[0].Columns.Add("MonthValue");
85
86 for (int i = 0; i < 12; i++)
87 {
88 DataRow dataRow = dataSet.Tables[0].NewRow();
89 dataRow["MonthText"] = i + 1;
90 dataRow["MonthValue"] = ((int)(i + 1)).ToString("00");
91 dataSet.Tables[0].Rows.Add(dataRow);
92 }
93 return dataSet;
94 }
95
96 /// <summary>
97 /// 取得某年周数据
98 /// </summary>
99 /// <param name="year">
100 /// 要取周数据的年份
101 /// </param>
102 /// <returns></returns>
103 public DataSet GetWeekData(string year)
104 {
105 DataSet dataSet = new DataSet();
106 dataSet.Tables.Add();
107 dataSet.Tables[0].Columns.Add("WeekText");
108 dataSet.Tables[0].Columns.Add("WeekValue");
109
110 //取得当年周数
111 int OneYearDays = (System.DateTime.IsLeapYear(Convert.ToInt32(year))) ? 366 : 365;
112 int weeks = Convert.ToInt32((double)System.Math.Ceiling((OneYearDays - FirstWeek(year)) / 7)) + 1;
113 for (int i = 0; i <= weeks; i++)
114 {
115 DataRow dataRow = dataSet.Tables[0].NewRow();
116 dataRow["WeekText"] = "第" + (i + 1) + "周";
117 dataRow["WeekValue"] = ((int)(i + 1)).ToString("000");
118 dataSet.Tables[0].Rows.Add(dataRow);
119 }
120
121 return dataSet;
122 }
123
124 /// <summary>
125 /// 取得某年某周的第一天及第七天的日期
126 /// </summary>
127 /// <param name="WeekIndex">
128 /// 周序号
129 /// </param>
130 /// <param name="year">
131 /// 年份
132 /// </param>
133 /// <returns>
134 /// string[] 格式为yyyy/MM/dd
135 /// str[0] 为第一天的日期
136 /// str[1] 为第七天的日期
137 /// </returns>
138 public string[] GetStartAndEndDayByWeekIndex(int WeekIndex, string year)
139 {
140 int EndDaysIndex = 7 - FirstWeek(year) + 1 + 7 * (WeekIndex - 1);
141 EndDaysIndex = (EndDaysIndex > (System.DateTime.IsLeapYear(Convert.ToInt32(year)) ? 366 : 365)) ? (System.DateTime.IsLeapYear(Convert.ToInt32(year)) ? 366 : 365) : EndDaysIndex;
142 string EndDay = GetDay(EndDaysIndex, year);
143
144 int StartDaysIndex = ((EndDaysIndex - 6) > 0) ? (EndDaysIndex - 6) : 1;
145 string StartDay = GetDay(StartDaysIndex, year);
146 string[] str = new string[2];
147 str[0] = StartDay;
148 str[1] = EndDay;
149 return str;
150 }
151
152
153 /// <summary>
154 /// 根据月份得到其为第几季度
155 /// </summary>
156 /// <param name="month">
157 /// 月份
158 /// </param>
159 /// <returns>
160 /// 季度,值为 1,2,3,4
161 /// </returns>
162 public string GetQuarterByMonth(string month)
163 {
164 string quarter = null;
165 switch (month)
166 {
167 case "01":
168 case "02":
169 case "03":
170 quarter = "1";
171 break;
172 case "04":
173 case "05":
174 case "06":
175 quarter = "2";
176 break;
177 case "07":
178 case "08":
179 case "09":
180 quarter = "3";
181 break;
182 case "10":
183 case "11":
184 case "12":
185 quarter = "4";
186 break;
187 }
188 return quarter;
189 }
190
191
192 /// <summary>
193 /// 根据两个日期来判断是否为同一个月,输入的天数格式为yyyy/MM/dd
194 /// </summary>
195 /// <param name="day1">日期1</param>
196 /// <param name="day2">日期2</param>
197 /// <returns>
198 /// 若相同,则str[0] = true;str[1] = month
199 /// 若不同,则str[0] = false;str[1] = month1;str[2] = month2;
200 /// </returns>
201
202 public string[] GetMonthByDay(string day1, string day2)
203 {
204 string[] str = new string[3];
205 if (day1.Split('/')[1] == day2.Split('/')[1])
206 {
207 str[0] = "true";
208 str[1] = day1.Split('/')[1];
209 }
210 else
211 {
212 str[0] = "false";
213 str[1] = day1.Split('/')[1];
214 str[2] = day2.Split('/')[1];
215 }
216 return str;
217 }
218
219 /// <summary>
220 /// 根据某年中的第daysindex天,取得那一天的日期,.
221 /// </summary>
222 /// <param name="DaysIndex">天数</param>
223 /// <param name="year">年份</param>
224 /// <returns>
225 /// 返回格式为yyyy/MM/dd
226 /// </returns>
227 private string GetDay(int DaysIndex, string year)
228 {
229 string date = null;
230
231 for (int i = 1; i <= 12; i++)
232 {
233 for (int j = 1; j <= GetDayByMonth(year, i); j++)
234 {
235 if (Convert.ToDateTime(year + "/" + i.ToString("00") + "/" + j.ToString("00")).DayOfYear == DaysIndex)
236 {
237 date = year + "/" + i.ToString("00") + "/" + j.ToString("00");
238 break;
239 }
240 }
241 }
242 return date;
243 }
244 /// <summary>
245 /// 取得对应月份的天数
246 /// </summary>
247 /// <param name="year">年份</param>
248 /// <param name="month">月份</param>
249 /// <returns>
250 /// 天数
251 /// </returns>
252 private int GetDayByMonth(string year, int month)
253 {
254 int day = 0;
255 switch (month)
256 {
257 case 1:
258 case 3:
259 case 5:
260 case 7:
261 case 8:
262 case 10:
263 case 12:
264 day = 31;
265 break;
266 case 2:
267 day = System.DateTime.IsLeapYear(Convert.ToInt32(year)) ? 29 : 28;
268 break;
269 default:
270 day = 30;
271 break;
272 }
273 return day;
274 }
275
276 /// <summary>
277 /// 取得某年第一天是周几
278 /// </summary>
279 /// <param name="year">年份</param>
280 /// <returns>
281 /// 返回数字。周一1,周日7
282 /// </returns>
283 private int FirstWeek(string year)
284 {
285 string date = year + "/01/01";
286 int day = 0;
287 switch (Convert.ToDateTime(date).DayOfWeek.ToString())
288 {
289 case "Monday":
290 day = 1;
291 break;
292 case "Thuesday":
293 day = 2;
294 break;
295 case "Wednesday":
296 day = 3;
297 break;
298 case "Thursday":
299 day = 4;
300 break;
301 case "Friday":
302 day = 5;
303 break;
304 case "Saturday":
305 day = 6;
306 break;
307 case "Sunday":
308 day = 7;
309 break;
310 default:
311 break;
312 }
313 return day;
314 }
315
316}
2{
3 public AboutDate()
4 {
5 //
6 // TODO: Add constructor logic here
7 //
8 }
9
10 /// <summary>
11 /// 取得近10年的数据
12 /// </summary>
13 /// <param name="info">
14 /// 偏移量(info为n,则包含 当前年+n 的年份)
15 /// </param>
16 /// <returns>
17 /// DataSet
18 ///</returns>
19
20 public DataSet GetYearData(int info)
21 {
22 DataSet dataSet = new DataSet();
23 dataSet.Tables.Add();
24 dataSet.Tables[0].Columns.Add("YearText");
25 dataSet.Tables[0].Columns.Add("YearValue");
26
27 for (int i = 10; i >= 0; i--)
28 {
29 DataRow dataRow = dataSet.Tables[0].NewRow();
30 dataRow["YearText"] = DateTime.Now.Year - i + info;
31 dataRow["YearValue"] = DateTime.Now.Year - i + info;
32 dataSet.Tables[0].Rows.Add(dataRow);
33 }
34 return dataSet;
35 }
36
37 /// <summary>
38 /// 取得季度数据
39 /// </summary>
40 /// <returns>
41 /// DataSet
42 /// </returns>
43 public DataSet GetQuarterData()
44 {
45 DataSet dataSet = new DataSet();
46 dataSet.Tables.Add();
47 dataSet.Tables[0].Columns.Add("QuarterText");
48 dataSet.Tables[0].Columns.Add("QuarterValue");
49 DataRow dataRow1 = dataSet.Tables[0].NewRow();
50 DataRow dataRow2 = dataSet.Tables[0].NewRow();
51 DataRow dataRow3 = dataSet.Tables[0].NewRow();
52 DataRow dataRow4 = dataSet.Tables[0].NewRow();
53
54 dataRow1["QuarterText"] = "第一季度";
55 dataRow1["QuarterValue"] = "1";
56
57 dataRow2["QuarterText"] = "第二季度";
58 dataRow2["QuarterValue"] = "2";
59
60 dataRow3["QuarterText"] = "第三季度";
61 dataRow3["QuarterValue"] = "3";
62
63 dataRow4["QuarterText"] = "第四季度";
64 dataRow4["QuarterValue"] = "4";
65
66 dataSet.Tables[0].Rows.Add(dataRow1);
67 dataSet.Tables[0].Rows.Add(dataRow2);
68 dataSet.Tables[0].Rows.Add(dataRow3);
69 dataSet.Tables[0].Rows.Add(dataRow4);
70 return dataSet;
71 }
72
73 /// <summary>
74 /// 取得月份数据,每年12个月
75 /// </summary>
76 /// <returns>
77 /// DataSet
78 /// </returns>
79 public DataSet GetMonthData()
80 {
81 DataSet dataSet = new DataSet();
82 dataSet.Tables.Add();
83 dataSet.Tables[0].Columns.Add("MonthText");
84 dataSet.Tables[0].Columns.Add("MonthValue");
85
86 for (int i = 0; i < 12; i++)
87 {
88 DataRow dataRow = dataSet.Tables[0].NewRow();
89 dataRow["MonthText"] = i + 1;
90 dataRow["MonthValue"] = ((int)(i + 1)).ToString("00");
91 dataSet.Tables[0].Rows.Add(dataRow);
92 }
93 return dataSet;
94 }
95
96 /// <summary>
97 /// 取得某年周数据
98 /// </summary>
99 /// <param name="year">
100 /// 要取周数据的年份
101 /// </param>
102 /// <returns></returns>
103 public DataSet GetWeekData(string year)
104 {
105 DataSet dataSet = new DataSet();
106 dataSet.Tables.Add();
107 dataSet.Tables[0].Columns.Add("WeekText");
108 dataSet.Tables[0].Columns.Add("WeekValue");
109
110 //取得当年周数
111 int OneYearDays = (System.DateTime.IsLeapYear(Convert.ToInt32(year))) ? 366 : 365;
112 int weeks = Convert.ToInt32((double)System.Math.Ceiling((OneYearDays - FirstWeek(year)) / 7)) + 1;
113 for (int i = 0; i <= weeks; i++)
114 {
115 DataRow dataRow = dataSet.Tables[0].NewRow();
116 dataRow["WeekText"] = "第" + (i + 1) + "周";
117 dataRow["WeekValue"] = ((int)(i + 1)).ToString("000");
118 dataSet.Tables[0].Rows.Add(dataRow);
119 }
120
121 return dataSet;
122 }
123
124 /// <summary>
125 /// 取得某年某周的第一天及第七天的日期
126 /// </summary>
127 /// <param name="WeekIndex">
128 /// 周序号
129 /// </param>
130 /// <param name="year">
131 /// 年份
132 /// </param>
133 /// <returns>
134 /// string[] 格式为yyyy/MM/dd
135 /// str[0] 为第一天的日期
136 /// str[1] 为第七天的日期
137 /// </returns>
138 public string[] GetStartAndEndDayByWeekIndex(int WeekIndex, string year)
139 {
140 int EndDaysIndex = 7 - FirstWeek(year) + 1 + 7 * (WeekIndex - 1);
141 EndDaysIndex = (EndDaysIndex > (System.DateTime.IsLeapYear(Convert.ToInt32(year)) ? 366 : 365)) ? (System.DateTime.IsLeapYear(Convert.ToInt32(year)) ? 366 : 365) : EndDaysIndex;
142 string EndDay = GetDay(EndDaysIndex, year);
143
144 int StartDaysIndex = ((EndDaysIndex - 6) > 0) ? (EndDaysIndex - 6) : 1;
145 string StartDay = GetDay(StartDaysIndex, year);
146 string[] str = new string[2];
147 str[0] = StartDay;
148 str[1] = EndDay;
149 return str;
150 }
151
152
153 /// <summary>
154 /// 根据月份得到其为第几季度
155 /// </summary>
156 /// <param name="month">
157 /// 月份
158 /// </param>
159 /// <returns>
160 /// 季度,值为 1,2,3,4
161 /// </returns>
162 public string GetQuarterByMonth(string month)
163 {
164 string quarter = null;
165 switch (month)
166 {
167 case "01":
168 case "02":
169 case "03":
170 quarter = "1";
171 break;
172 case "04":
173 case "05":
174 case "06":
175 quarter = "2";
176 break;
177 case "07":
178 case "08":
179 case "09":
180 quarter = "3";
181 break;
182 case "10":
183 case "11":
184 case "12":
185 quarter = "4";
186 break;
187 }
188 return quarter;
189 }
190
191
192 /// <summary>
193 /// 根据两个日期来判断是否为同一个月,输入的天数格式为yyyy/MM/dd
194 /// </summary>
195 /// <param name="day1">日期1</param>
196 /// <param name="day2">日期2</param>
197 /// <returns>
198 /// 若相同,则str[0] = true;str[1] = month
199 /// 若不同,则str[0] = false;str[1] = month1;str[2] = month2;
200 /// </returns>
201
202 public string[] GetMonthByDay(string day1, string day2)
203 {
204 string[] str = new string[3];
205 if (day1.Split('/')[1] == day2.Split('/')[1])
206 {
207 str[0] = "true";
208 str[1] = day1.Split('/')[1];
209 }
210 else
211 {
212 str[0] = "false";
213 str[1] = day1.Split('/')[1];
214 str[2] = day2.Split('/')[1];
215 }
216 return str;
217 }
218
219 /// <summary>
220 /// 根据某年中的第daysindex天,取得那一天的日期,.
221 /// </summary>
222 /// <param name="DaysIndex">天数</param>
223 /// <param name="year">年份</param>
224 /// <returns>
225 /// 返回格式为yyyy/MM/dd
226 /// </returns>
227 private string GetDay(int DaysIndex, string year)
228 {
229 string date = null;
230
231 for (int i = 1; i <= 12; i++)
232 {
233 for (int j = 1; j <= GetDayByMonth(year, i); j++)
234 {
235 if (Convert.ToDateTime(year + "/" + i.ToString("00") + "/" + j.ToString("00")).DayOfYear == DaysIndex)
236 {
237 date = year + "/" + i.ToString("00") + "/" + j.ToString("00");
238 break;
239 }
240 }
241 }
242 return date;
243 }
244 /// <summary>
245 /// 取得对应月份的天数
246 /// </summary>
247 /// <param name="year">年份</param>
248 /// <param name="month">月份</param>
249 /// <returns>
250 /// 天数
251 /// </returns>
252 private int GetDayByMonth(string year, int month)
253 {
254 int day = 0;
255 switch (month)
256 {
257 case 1:
258 case 3:
259 case 5:
260 case 7:
261 case 8:
262 case 10:
263 case 12:
264 day = 31;
265 break;
266 case 2:
267 day = System.DateTime.IsLeapYear(Convert.ToInt32(year)) ? 29 : 28;
268 break;
269 default:
270 day = 30;
271 break;
272 }
273 return day;
274 }
275
276 /// <summary>
277 /// 取得某年第一天是周几
278 /// </summary>
279 /// <param name="year">年份</param>
280 /// <returns>
281 /// 返回数字。周一1,周日7
282 /// </returns>
283 private int FirstWeek(string year)
284 {
285 string date = year + "/01/01";
286 int day = 0;
287 switch (Convert.ToDateTime(date).DayOfWeek.ToString())
288 {
289 case "Monday":
290 day = 1;
291 break;
292 case "Thuesday":
293 day = 2;
294 break;
295 case "Wednesday":
296 day = 3;
297 break;
298 case "Thursday":
299 day = 4;
300 break;
301 case "Friday":
302 day = 5;
303 break;
304 case "Saturday":
305 day = 6;
306 break;
307 case "Sunday":
308 day = 7;
309 break;
310 default:
311 break;
312 }
313 return day;
314 }
315
316}
The code has build succeeded in vs2003.