一、前言最近工作比較忙,,與此同時(shí)自己也在業(yè)余時(shí)間開(kāi)發(fā)一個(gè)電子商務(wù)網(wǎng)站,。雖然每天都很累,但感覺(jué)過(guò)的相當(dāng)充實(shí)。由于時(shí)間緊張,,最近沒(méi)有經(jīng)常來(lái)園子,,只是有人留言的時(shí)候過(guò)來(lái)回復(fù)下。今天過(guò)來(lái)刷刷存在感,。 二,、應(yīng)用場(chǎng)景舉例1.按指定日期查詢 (1)用戶在UI上選擇日期; (2)系統(tǒng)計(jì)算出今天的起始時(shí)間(****年**月**日 00:00:00)和今天的結(jié)束時(shí)間(****年**月**日 23:59:59); (3)系統(tǒng)根據(jù)根據(jù)該日期范圍到數(shù)據(jù)庫(kù)取出該日期范圍的數(shù)據(jù)。 如下圖所示,,查詢指定日期的航班及機(jī)票數(shù)量: 2.按時(shí)間范圍查詢 (1)用戶在UI上選擇“起始日期”和“結(jié)束日期”; (2)系統(tǒng)計(jì)算出開(kāi)始日期的起始時(shí)間(****年**月**日 00:00:00); (2)系統(tǒng)計(jì)算出結(jié)束日期的結(jié)束時(shí)間(****年**月**日 23:59:59); (3)系統(tǒng)根據(jù)根據(jù)該日期范圍到數(shù)據(jù)庫(kù)取出該日期范圍的數(shù)據(jù),。 如下圖所示,比較常見(jiàn)的按日期查詢: 3.按日,、月,、周、年查詢(此處以按月查詢?yōu)槔?/p> (1)系統(tǒng)取出當(dāng)前日期; (2)系統(tǒng)根據(jù)當(dāng)前日期計(jì)算出本月的開(kāi)始日期的起始時(shí)間(****年**月**日 00:00:00); (3)系統(tǒng)根據(jù)當(dāng)前日期計(jì)算出本月的結(jié)束日期的結(jié)束時(shí)間(****年**月**日 23:59:59); (4)系統(tǒng)根據(jù)根據(jù)該日期范圍到數(shù)據(jù)庫(kù)取出該日期范圍的數(shù)據(jù),。 如下圖所示,,比較常見(jiàn)的月報(bào)表 4.最終效果 三、代碼及下載1 /* 2 * 3 * 創(chuàng)建人:李林峰 4 * 5 * 時(shí) 間:2009-05-04 6 * 7 * 描 述:日期轉(zhuǎn)換幫助類,,常用于報(bào)表開(kāi)發(fā); 8 * 可以轉(zhuǎn)換成為:本日開(kāi)始時(shí)間,、本日結(jié)束時(shí)間、本周開(kāi)始時(shí)間,、本周結(jié)束時(shí)間,、本月開(kāi)始時(shí)間、本月結(jié)束時(shí)間,、本年開(kāi)始時(shí)間,、本年結(jié)束時(shí)間 9 * 10 */ 11 12 using System; 13 14 namespace SongCai8.WebSite 15 { 16 /// <summary> 17 /// 日期轉(zhuǎn)換幫助類 18 /// </summary> 19 public static class DateTimeHelper 20 { 21 #region 日 22 /// <summary> 23 /// 將日期轉(zhuǎn)換為本日的開(kāi)始時(shí)間 24 /// </summary> 25 /// <param name="value">2001-01-01</param> 26 /// <returns>2001-01-01 00:00:00</returns> 27 public static DateTime ToDayStart(string value) 28 { 29 //轉(zhuǎn)換成日期類型 30 DateTime date = System.Convert.ToDateTime(value); 31 //轉(zhuǎn)換成短日期類型字符 32 string shortDate = date.ToShortDateString(); 33 //返回日期類型 34 return System.Convert.ToDateTime(shortDate); 35 } 36 37 /// <summary> 38 /// 將日期轉(zhuǎn)換為本日的開(kāi)始時(shí)間 39 /// </summary> 40 /// <param name="value">任意時(shí)間</param> 41 /// <returns>2001-01-01 00:00:00</returns> 42 public static DateTime ToDayStart(DateTime value) 43 { 44 //轉(zhuǎn)換成短日期類型字符 45 string shortDate = value.ToShortDateString(); 46 //返回日期類型 47 return System.Convert.ToDateTime(shortDate); 48 } 49 50 /// <summary> 51 /// 將日期轉(zhuǎn)換為本日的開(kāi)始時(shí)間 52 /// </summary> 53 /// <param name="value">2001-01-01</param> 54 /// <returns>2001-01-01 23:59:59</returns> 55 public static DateTime ToDayEnd(string value) 56 { 57 //轉(zhuǎn)換成日期類型 58 DateTime date = System.Convert.ToDateTime(value); 59 //轉(zhuǎn)換成短日期類型 60 DateTime shortDate = System.Convert.ToDateTime(date.ToShortDateString()); 61 //返回日期加一天減一秒 62 return shortDate.AddDays(1).AddSeconds(-1); 63 } 64 65 /// <summary> 66 /// 將日期轉(zhuǎn)換為本日的結(jié)束時(shí)間 67 /// </summary> 68 /// <param name="value">任意時(shí)間</param> 69 /// <returns>2001-01-01 23:59:59</returns> 70 public static DateTime ToDayEnd(DateTime value) 71 { 72 //轉(zhuǎn)換成短日期類型 73 DateTime shortDate = System.Convert.ToDateTime(value.ToShortDateString()); 74 //返回日期加一天減一秒 75 return shortDate.AddDays(1).AddSeconds(-1); 76 } 77 #endregion 78 79 #region 周 80 /// <summary> 81 /// 將日期轉(zhuǎn)換為本周的開(kāi)始時(shí)間 82 /// </summary> 83 /// <param name="value">2001-01-01</param> 84 /// <returns>2001-01-01 00:00:00</returns> 85 public static DateTime ToWeekStart(string value) 86 { 87 //轉(zhuǎn)換成日期類型 88 DateTime date = System.Convert.ToDateTime(value); 89 //根據(jù)當(dāng)前時(shí)間取出該周周一的當(dāng)前時(shí)間 90 DateTime weekStart = ToWeekStart(date, date.Date.DayOfWeek); 91 //轉(zhuǎn)換成短日期類型字符 92 string shortDate = weekStart.ToShortDateString(); 93 //返回日期類型 94 return System.Convert.ToDateTime(shortDate); 95 } 96 97 /// <summary> 98 /// 將日期轉(zhuǎn)換為本周的開(kāi)始時(shí)間 99 /// </summary> 100 /// <param name="value">任意時(shí)間</param> 101 /// <returns>2001-01-01 00:00:00</returns> 102 public static DateTime ToWeekStart(DateTime value) 103 { 104 //根據(jù)當(dāng)前時(shí)間取出該周周一的當(dāng)前時(shí)間 105 DateTime weekStart = ToWeekStart(value, value.Date.DayOfWeek); 106 //轉(zhuǎn)換成短日期類型字符 107 string shortDate = weekStart.ToShortDateString(); 108 //返回日期類型 109 return System.Convert.ToDateTime(shortDate); 110 } 111 112 /// <summary> 113 /// 將日期轉(zhuǎn)換為本周的結(jié)束時(shí)間 114 /// </summary> 115 /// <param name="value">2001-01-01</param> 116 /// <returns>2001-01-01 23:59:59</returns> 117 public static DateTime ToWeekEnd(string value) 118 { 119 //轉(zhuǎn)換成日期類型 120 DateTime date = System.Convert.ToDateTime(value); 121 //根據(jù)當(dāng)前時(shí)間取出該周周末的當(dāng)前時(shí)間 122 DateTime weekEnd = ToWeekEnd(date, date.Date.DayOfWeek); 123 //轉(zhuǎn)換成短日期類型字符 124 string shortDate = weekEnd.ToShortDateString(); 125 //返回日期加一天減一秒 126 return Convert.ToDateTime(shortDate).AddDays(1).AddSeconds(-1); 127 } 128 129 /// <summary> 130 /// 將日期轉(zhuǎn)換為本周的結(jié)束時(shí)間 131 /// </summary> 132 /// <param name="value">任意時(shí)間</param> 133 /// <returns>2001-01-01 23:59:59</returns> 134 public static DateTime ToWeekEnd(DateTime value) 135 { 136 //根據(jù)當(dāng)前時(shí)間取出該周周末的當(dāng)前時(shí)間 137 DateTime weekEnd = ToWeekEnd(value, value.Date.DayOfWeek); 138 //轉(zhuǎn)換成短日期類型字符 139 string shortDate = weekEnd.ToShortDateString(); 140 //返回日期類型 141 //返回日期加一天減一秒 142 return Convert.ToDateTime(shortDate).AddDays(1).AddSeconds(-1); 143 } 144 145 /// <summary> 146 /// 將日期轉(zhuǎn)換為本周周一的某一時(shí)間 147 /// </summary> 148 /// <param name="date">將要轉(zhuǎn)換的日期</param> 149 /// <param name="week">傳入日期的周的枚舉類型</param> 150 /// <returns>2001-01-01 12:12:12</returns> 151 private static DateTime ToWeekStart(DateTime date, DayOfWeek week) 152 { 153 DateTime WeekStart = new DateTime(); 154 switch (week) 155 { 156 case DayOfWeek.Monday: 157 WeekStart = date; 158 break; 159 case DayOfWeek.Tuesday: 160 WeekStart = date.AddDays(-1); 161 break; 162 case DayOfWeek.Wednesday: 163 WeekStart = date.AddDays(-2); 164 break; 165 case DayOfWeek.Thursday: 166 WeekStart = date.AddDays(-3); 167 break; 168 case DayOfWeek.Friday: 169 WeekStart = date.AddDays(-4); 170 break; 171 case DayOfWeek.Saturday: 172 WeekStart = date.AddDays(-5); 173 break; 174 case DayOfWeek.Sunday: 175 WeekStart = date.AddDays(-6); 176 break; 177 } 178 return WeekStart; 179 } 180 181 /// <summary> 182 /// 將日期轉(zhuǎn)換為本周周日的某一時(shí)間 183 /// </summary> 184 /// <param name="date">將要轉(zhuǎn)換的日期</param> 185 /// <param name="week">傳入日期的周的枚舉類型</param> 186 /// <returns>2001-01-01 12:12:12</returns> 187 private static DateTime ToWeekEnd(DateTime date, DayOfWeek week) 188 { 189 DateTime WeekStart = new DateTime(); 190 switch (week) 191 { 192 case DayOfWeek.Monday: 193 WeekStart = date.AddDays(6); 194 break; 195 case DayOfWeek.Tuesday: 196 WeekStart = date.AddDays(5); 197 break; 198 case DayOfWeek.Wednesday: 199 WeekStart = date.AddDays(4); 200 break; 201 case DayOfWeek.Thursday: 202 WeekStart = date.AddDays(3); 203 break; 204 case DayOfWeek.Friday: 205 WeekStart = date.AddDays(2); 206 break; 207 case DayOfWeek.Saturday: 208 WeekStart = date.AddDays(1); 209 break; 210 case DayOfWeek.Sunday: 211 WeekStart = date; 212 break; 213 } 214 return WeekStart; 215 } 216 217 #endregion 218 219 #region 月 220 /// <summary> 221 /// 將日期轉(zhuǎn)換為本月的開(kāi)始時(shí)間 222 /// </summary> 223 /// <param name="value">2001-01-01</param> 224 /// <returns>2001-01-01 00:00:00</returns> 225 public static DateTime ToMonthStart(string value) 226 { 227 //轉(zhuǎn)換成日期類型 228 DateTime date = System.Convert.ToDateTime(value); 229 //根據(jù)年,、月重新創(chuàng)建日期 230 return new DateTime(date.Year, date.Month, 1); 231 } 232 233 /// <summary> 234 /// 將日期轉(zhuǎn)換為本月的開(kāi)始時(shí)間 235 /// </summary> 236 /// <param name="value">任意時(shí)間</param> 237 /// <returns>2001-01-01 00:00:00</returns> 238 public static DateTime ToMonthStart(DateTime value) 239 { 240 //根據(jù)年,、月重新創(chuàng)建日期 241 return new DateTime(value.Year, value.Month, 1); 242 } 243 244 /// <summary> 245 /// 將日期轉(zhuǎn)換為本月的結(jié)束時(shí)間 246 /// </summary> 247 /// <param name="value">2001-01-01</param> 248 /// <returns>2001-01-31 23:59:59</returns> 249 public static DateTime ToMonthEnd(string value) 250 { 251 //轉(zhuǎn)換成日期類型 252 DateTime date = System.Convert.ToDateTime(value); 253 //根據(jù)年、月重新創(chuàng)建日期 254 DateTime monthStart = new DateTime(date.Year, date.Month, 1); 255 //創(chuàng)建結(jié)束日期 256 return monthStart.AddMonths(1).AddSeconds(-1); 257 } 258 259 /// <summary> 260 /// 將日期轉(zhuǎn)換為本月的結(jié)束時(shí)間 261 /// </summary> 262 /// <param name="value">任意時(shí)間</param> 263 /// <returns>2001-01-31 23:59:59</returns> 264 public static DateTime ToMonthEnd(DateTime value) 265 { 266 //根據(jù)年,、月重新創(chuàng)建日期 267 DateTime monthStart = new DateTime(value.Year, value.Month, 1); 268 //創(chuàng)建結(jié)束日期 269 return monthStart.AddMonths(1).AddSeconds(-1); 270 } 271 #endregion 272 273 #region 年 274 /// <summary> 275 /// 將日期轉(zhuǎn)換為本年的開(kāi)始時(shí)間 276 /// </summary> 277 /// <param name="value">2001-01-01</param> 278 /// <returns>2001-01-01 00:00:00</returns> 279 public static DateTime ToYearStart(string value) 280 { 281 //轉(zhuǎn)換成日期類型 282 DateTime date = System.Convert.ToDateTime(value); 283 //根據(jù)年,、月重新創(chuàng)建日期 284 return new DateTime(date.Year, 1, 1); 285 } 286 287 /// <summary> 288 /// 將日期轉(zhuǎn)換為本年的開(kāi)始時(shí)間 289 /// </summary> 290 /// <param name="value">任意時(shí)間</param> 291 /// <returns>2001-01-01 00:00:00</returns> 292 public static DateTime ToYearStart(DateTime value) 293 { 294 //根據(jù)年、月重新創(chuàng)建日期 295 return new DateTime(value.Year, 1, 1); 296 } 297 298 /// <summary> 299 /// 將日期轉(zhuǎn)換為本年的結(jié)束時(shí)間 300 /// </summary> 301 /// <param name="value">2001-01-01</param> 302 /// <returns>2001-12-31 23:59:59</returns> 303 public static DateTime ToYearEnd(string value) 304 { 305 //轉(zhuǎn)換成日期類型 306 DateTime date = System.Convert.ToDateTime(value); 307 //根據(jù)年,、月重新創(chuàng)建日期 308 DateTime yearStart = new DateTime(date.Year, 1, 1); 309 //創(chuàng)建結(jié)束日期 310 DateTime yearEnd = new DateTime(date.Year, 1, 1).AddYears(1).AddSeconds(-1); 311 return yearEnd; 312 } 313 314 /// <summary> 315 /// 將日期轉(zhuǎn)換為本年的結(jié)束時(shí)間 316 /// </summary> 317 /// <param name="value">任意時(shí)間</param> 318 /// <returns>2001-12-31 23:59:59</returns> 319 public static DateTime ToYearEnd(DateTime value) 320 { 321 //根據(jù)年,、月重新創(chuàng)建日期 322 DateTime yearStart = new DateTime(value.Year, 1, 1); 323 //創(chuàng)建結(jié)束日期 324 return new DateTime(value.Year, 1, 1).AddYears(1).AddSeconds(-1); 325 } 326 #endregion 327 } 328 }
版權(quán):http://www.cnblogs.com/iamlilinfeng
活到老,學(xué)到老,練到老...
|
|