首页 > 代码库 > 日期转换类

日期转换类

   1:   
   2:  // 创建日期: 2008-04-23
   3:  // 说    明: 日期,时间处理相关的类,大多转换为字符串。
   4:  using System;
   5:  using System.Collections.Generic;
   6:  using System.Text;
   7:   
   8:  namespace AnkeEdu.Tools
   9:  {
  10:      /// <summary>
  11:      /// 日期,时间处理
  12:      /// </summary>
  13:      public static class TimeHelper
  14:      {
  15:          /// <summary>
  16:          /// 返回当前时间的字符串表示 yyyy-MM-dd HH:mm:ss
  17:          /// </summary>
  18:          /// <returns>当前时间的字符串表示 yyyy-MM-dd HH:mm:ss</returns>
  19:          public static string GetNowStr()
  20:          {
  21:              return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  22:          }
  23:   
  24:          #region GetTimeFlag 根据时间标记返回时间字符表示形式(一天分为48段,0-47)
  25:          /// <summary>
  26:          /// 获到时间标记
  27:          /// </summary>
  28:          /// <param name="flag">时间标记对象</param>
  29:          /// <returns>返回时间字符表示</returns>
  30:          public static string GetTimeFlag(object flag)
  31:          {
  32:              if (flag == null) return "";
  33:              int nflag = Convert.ToInt32(flag.ToString());
  34:              return GetTimeFlag(nflag);
  35:          }
  36:          /// <summary>
  37:          /// 根据时间标记返回时间字符表示形式
  38:          /// 一天分为48段
  39:          /// 0:00-0:30 为0
  40:          /// </summary>
  41:          /// <param name="flag">时间标记</param>
  42:          /// <returns>返回标记后的时间字符串</returns>
  43:          public static string GetTimeFlag(int flag)
  44:          {
  45:              int hh = flag / 2;
  46:              int mm = ((flag % 2) != 0) ? 30 : 0;
  47:              DateTime d = new DateTime(1, 1, 1, hh, mm, 0);
  48:   
  49:              return d.ToString("HH:mm-") + d.AddMinutes(30).ToString("HH:mm");
  50:          }
  51:          #endregion
  52:          #region GetTimeFlagStartTime 根据时间标记返回时间段开始时间表示形式(一天分为48段,0-47)
  53:          /// <summary>
  54:          /// 根据时间标记返回时间段开始时间表示形式
  55:          /// 一天分为48段
  56:          /// 0:00-0:30 为0
  57:          /// </summary>
  58:          /// <param name="flag">时间标记</param>
  59:          /// <returns>返回标记后的时间</returns>
  60:          public static DateTime GetTimeFlagStartTime(int flag)
  61:          {
  62:              int hh = flag / 2;
  63:              int mm = ((flag % 2) != 0) ? 30 : 0;
  64:              DateTime d = new DateTime(1, 1, 1, hh, mm, 0);
  65:              return d;
  66:          }
  67:          #endregion
  68:   
  69:          #region GetCnDate 格式化日期为 yyyy-MM-dd 星期一
  70:          /// <summary>
  71:          /// 获取中午日期名 格式化日期为 yyyy-MM-dd 星期一
  72:          /// </summary>
  73:          /// <param name="dt">日期</param>
  74:          /// <returns>格式化的中午日期</returns>
  75:          public static string GetCnDate(DateTime dt)
  76:          {
  77:              return dt.ToString("yyyy-MM-dd") + " " + GetCnWeek(dt);
  78:          }
  79:          #endregion
  80:          #region GetMonthStr 转换月份为英文格式
  81:          /// <summary>
  82:          /// 转换月份为英文格式
  83:          /// </summary>
  84:          /// <param name="month">月份</param>
  85:          /// <returns>英文月份</returns>
  86:          public static string GetMonthStr(int month)
  87:          {
  88:              switch (month)
  89:              {
  90:                  case 1: return "January";
  91:                  case 2: return "February";
  92:                  case 3: return "March";
  93:                  case 4: return "April";
  94:                  case 5: return "May";
  95:                  case 6: return "June";
  96:                  case 7: return "July";
  97:                  case 8: return "August";
  98:                  case 9: return "September";
  99:                  case 10: return "October";
 100:                  case 11: return "November";
 101:                  case 12: return "December";
 102:                  default: return "January";
 103:              }
 104:   
 105:          }
 106:          #endregion
 107:          #region FormatDate 格式化日期为yyyy-MM-dd
 108:          /// <summary>
 109:          /// 格式化日期为2006-12-22
 110:          /// </summary>
 111:          /// <param name="dTime">日期</param>
 112:          /// <returns>返回格式化后的日期字符串</returns>
 113:          public static string FormatDate(DateTime? dTime)
 114:          {
 115:              string rStr;
 116:              rStr = dTime.Value.Year + "-" + dTime.Value.Month + "-" + dTime.Value.Day;
 117:              return rStr;
 118:          }
 119:          /// <summary>
 120:          /// 格式化日期为2006-12-22
 121:          /// </summary>
 122:          /// <param name="dTime">日期</param>
 123:          /// <returns>返回格式化后的日期字符串</returns>
 124:          public static string FormatDateYMD(DateTime? dTime)
 125:          {
 126:              if (dTime == null) return "1970-01-01";
 127:   
 128:              return dTime.Value.ToString("yyyy-MM-dd");
 129:          }
 130:          #endregion
 131:          #region GetCnWeek 获取中文星期名
 132:          /// <summary>
 133:          /// 获取中文星期名
 134:          /// </summary>
 135:          /// <param name="d">传入的整数</param>
 136:          /// <returns>返回中文星期名</returns>
 137:          public static string GetCnWeek(int d)
 138:          {
 139:              string rStr = "";
 140:              switch (d)
 141:              {
 142:                  case 7:
 143:                      rStr = "星期日";
 144:                      break;
 145:                  case 1:
 146:                      rStr = "星期一";
 147:                      break;
 148:                  case 2:
 149:                      rStr = "星期二";
 150:                      break;
 151:                  case 3:
 152:                      rStr = "星期三";
 153:                      break;
 154:                  case 4:
 155:                      rStr = "星期四";
 156:                      break;
 157:                  case 5:
 158:                      rStr = "星期五";
 159:                      break;
 160:                  case 6:
 161:                      rStr = "星期六";
 162:                      break;
 163:              }
 164:              return rStr;
 165:          }
 166:          #endregion
 167:          #region GetCnWeek 获取中文星期名
 168:          /// <summary>
 169:          /// 获取中文星期名
 170:          /// </summary>
 171:          /// <param name="sDate">日期</param>
 172:          /// <returns>返回对应的中文星期名</returns>
 173:          public static string GetCnWeek(DateTime sDate)
 174:          {
 175:              string rStr = "";
 176:              switch (sDate.DayOfWeek)
 177:              {
 178:                  case DayOfWeek.Sunday:
 179:                      rStr = "星期日";
 180:                      break;
 181:                  case DayOfWeek.Monday:
 182:                      rStr = "星期一";
 183:                      break;
 184:                  case DayOfWeek.Tuesday:
 185:                      rStr = "星期二";
 186:                      break;
 187:                  case DayOfWeek.Wednesday:
 188:                      rStr = "星期三";
 189:                      break;
 190:                  case DayOfWeek.Thursday:
 191:                      rStr = "星期四";
 192:                      break;
 193:                  case DayOfWeek.Friday:
 194:                      rStr = "星期五";
 195:                      break;
 196:                  case DayOfWeek.Saturday:
 197:                      rStr = "星期六";
 198:                      break;
 199:              }
 200:              return rStr;
 201:          }
 202:          #endregion
 203:          #region GetEngWeek 获取英文星期名
 204:          /// <summary>
 205:          /// 获取英文星期名
 206:          /// </summary>
 207:          /// <param name="sDate">日期</param>
 208:          /// <returns>返回星期名</returns>
 209:          public static string GetEngWeek(DateTime sDate)
 210:          {
 211:              string rStr = "";
 212:              switch (sDate.DayOfWeek)
 213:              {
 214:                  case DayOfWeek.Sunday:
 215:                      rStr = "Sunday";
 216:                      break;
 217:                  case DayOfWeek.Monday:
 218:                      rStr = "Monday";
 219:                      break;
 220:                  case DayOfWeek.Tuesday:
 221:                      rStr = "Tuesday";
 222:                      break;
 223:                  case DayOfWeek.Wednesday:
 224:                      rStr = "Wednesday";
 225:                      break;
 226:                  case DayOfWeek.Thursday:
 227:                      rStr = "Thursday";
 228:                      break;
 229:                  case DayOfWeek.Friday:
 230:                      rStr = "Friday";
 231:                      break;
 232:                  case DayOfWeek.Saturday:
 233:                      rStr = "Saturday";
 234:                      break;
 235:              }
 236:              return rStr;
 237:          }
 238:          #endregion
 239:          #region GetCnWeek 获取DayOfWeek
 240:          /// <summary>
 241:          /// 获取DayOfWeek
 242:          /// </summary>
 243:          /// <param name="sDate">日期</param>
 244:          /// <returns>返回星期名称对应的整数</returns>
 245:          public static int GetDayOfWeek(DateTime sDate)
 246:          {
 247:              int dayOfWeek = 0;
 248:              switch (sDate.DayOfWeek)
 249:              {
 250:                  case DayOfWeek.Sunday:
 251:                      dayOfWeek = 0;
 252:                      break;
 253:                  case DayOfWeek.Monday:
 254:                      dayOfWeek = 1;
 255:                      break;
 256:                  case DayOfWeek.Tuesday:
 257:                      dayOfWeek = 2;
 258:                      break;
 259:                  case DayOfWeek.Wednesday:
 260:                      dayOfWeek = 3;
 261:                      break;
 262:                  case DayOfWeek.Thursday:
 263:                      dayOfWeek = 4;
 264:                      break;
 265:                  case DayOfWeek.Friday:
 266:                      dayOfWeek = 5;
 267:                      break;
 268:                  case DayOfWeek.Saturday:
 269:                      dayOfWeek = 6;
 270:                      break;
 271:              }
 272:              return dayOfWeek;
 273:          }
 274:          #endregion
 275:          #region GetDataTimeStr 获取字符串格式的日期时间 yyyy-MM-dd HH:mm:ss
 276:          /// <summary>
 277:          /// 获取字符串格式的日期时间 yyyy-MM-dd HH:mm:ss
 278:          /// </summary>
 279:          /// <param name="dateTime">要格式的时间</param>
 280:          /// <returns>返回格式化后的时间字符串</returns>
 281:          public static string GetDataTimeStr(DateTime dateTime)
 282:          {
 283:              return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
 284:          }
 285:          #endregion
 286:          #region GetDataStr 获取字符串格式的日期 yyyy-MM-dd
 287:          /// <summary>
 288:          /// 获取字符串格式的日期 yyyy-MM-dd
 289:          /// </summary>
 290:          /// <param name="dateTime">要格式的日期</param>
 291:          /// <returns>返回格式化后的时间字符串</returns>
 292:          public static string GetDataStr(DateTime dateTime)
 293:          {
 294:              return dateTime.ToString("yyyy-MM-dd");
 295:          }
 296:          #endregion
 297:          #region GetDateFormString 从一个字符串类型配合日期模板获取表示的日期
 298:          /// <summary>
 299:          /// 系统空值时间
 300:          /// </summary>
 301:          static public readonly DateTime SYS_NULL_TIME = new DateTime(1000, 1, 1);
 302:          /// <summary>
 303:          /// 从一个字符串类型配合日期模板获取表示的日期
 304:          /// </summary>
 305:          /// <param name="strDate">字符串表示的日期</param>
 306:          /// <param name="dateFormat">日期格式</param>        
 307:          /// <returns>返回的日期(如果异常返回默认值)</returns>
 308:          public static DateTime GetDateFormString(string strDate, string dateFormat)
 309:          {
 310:              if (string.IsNullOrEmpty(strDate))
 311:              {
 312:                  return SYS_NULL_TIME;
 313:              }
 314:   
 315:              try
 316:              {
 317:                  int year = GetDateIntFormString(dateFormat, strDate, ‘y‘);
 318:                  int month = GetDateIntFormString(dateFormat, strDate, ‘M‘);
 319:                  int day = GetDateIntFormString(dateFormat, strDate, ‘d‘);
 320:   
 321:                  return new DateTime(year, month, day);
 322:              }
 323:              catch
 324:              {
 325:                  return new DateTime();
 326:              }
 327:          }
 328:          /// <summary>
 329:          /// 从日期中获取日期
 330:          /// </summary>
 331:          /// <param name="dateForamt">时间格式</param>
 332:          /// <param name="dateStr">字符串日期</param>
 333:          /// <param name="ch">字符串类型:‘y’,‘m‘,‘d‘</param>
 334:          /// <returns>返回格式化后的整数</returns>
 335:          private static int GetDateIntFormString(string dateForamt, string dateStr, char ch)
 336:          {
 337:              //模板可能是变动的,从模板根据最后的ch值确定表示时间的区间
 338:              int indStart = dateForamt.IndexOf(ch);
 339:              int indEnd = dateForamt.LastIndexOf(ch);
 340:              if (dateStr.Length < indStart + 1)
 341:              {
 342:                  return 1;
 343:              }
 344:   
 345:              string tmpStr = "";
 346:   
 347:              if (dateStr.Length < indEnd + 1)
 348:              {
 349:                  tmpStr = dateStr.Substring(indStart);
 350:              }
 351:              else
 352:              {
 353:                  tmpStr = dateStr.Substring(indStart, indEnd - indStart + 1);
 354:              }
 355:   
 356:              return int.Parse(tmpStr);
 357:          }
 358:          #endregion
 359:   
 360:          #region ConvertDateStrToDateTime 转换一个字符串(yyyy-MM-dd)为时间格式
 361:          /// <summary>
 362:          /// 转换一个字符串(yyyy-MM-dd)为时间格式
 363:          /// </summary>
 364:          /// <param name="yyyy_MM_dd">年月日,应为yyyy-MM-dd</param>
 365:          /// <returns>返回转换后的时间格式</returns>
 366:          public static DateTime ConvertDateStrToDateTime(string yyyy_MM_dd)
 367:          {
 368:              return ConvertDateTimeStrToDateTime(yyyy_MM_dd, "00:00:00");
 369:          }
 370:          #endregion
 371:          #region ConvertDateTimeStrToDateTime 转换一个字符串(yyyy-MM-dd HH:mm:ss)为时间格式
 372:          /// <summary>
 373:          /// 转换一个字符串(yyyy-MM-dd HH:mm:ss)为时间格式
 374:          /// </summary>
 375:          /// <param name="yyyy_MM_dd">年月日,应为yyyy-MM-dd</param>
 376:          /// <param name="hh_mm_ss">时分秒,应为hh:mm:ss</param>
 377:          /// <returns>返回转换后的时间格式</returns>
 378:          public static DateTime ConvertDateTimeStrToDateTime(string yyyy_MM_dd, string hh_mm_ss)
 379:          {
 380:              if (yyyy_MM_dd.Length != 10 || hh_mm_ss.Length != 8)
 381:              {
 382:                  return new DateTime();
 383:              }
 384:              string[] datestr = yyyy_MM_dd.Split(‘-‘);
 385:              string[] timestr = hh_mm_ss.Split(‘:‘);
 386:   
 387:              return new DateTime(Int32.Parse(datestr[0]),
 388:                  Int32.Parse(datestr[1]),
 389:                  Int32.Parse(datestr[2]),
 390:                  Int32.Parse(timestr[0]),
 391:                  Int32.Parse(timestr[1]),
 392:                  Int32.Parse(timestr[2])
 393:                  );
 394:          }
 395:          #endregion
 396:   
 397:          #region FormatTimeByMillisecond 格式化毫秒时间长度为友好时间长度
 398:          /// <summary>
 399:          /// 格式化毫秒时间长度为友好时间长度
 400:          /// </summary>
 401:          /// <param name="pMillisecond">要格式化的毫秒</param>
 402:          /// <returns>天,小时,分,秒等</returns>
 403:          public static string FormatTimeByMillisecond(long pMillisecond)
 404:          {
 405:              StringBuilder builder = new StringBuilder();
 406:              pMillisecond = int.Parse(string.Format("{0:F0}", pMillisecond / 0x3e8L));
 407:              if (pMillisecond >= 0x15180L)
 408:              {
 409:                  builder.Append(string.Format("{0:F0}", pMillisecond / 0x15180L));
 410:                  builder.Append("天");
 411:                  pMillisecond = pMillisecond % 0x15180L;
 412:              }
 413:              if ((pMillisecond > 0xe10L) || (pMillisecond == 0L))
 414:              {
 415:                  builder.Append(string.Format("{0:F0}", pMillisecond / 0xe10L));
 416:                  builder.Append("小时");
 417:                  pMillisecond = pMillisecond % 0xe10L;
 418:              }
 419:              if ((pMillisecond > 60L) || (pMillisecond == 0L))
 420:              {
 421:                  builder.Append(string.Format("{0:F0}", pMillisecond / 60L));
 422:                  builder.Append("分");
 423:                  pMillisecond = pMillisecond % 60L;
 424:              }
 425:              if (pMillisecond < 60L)
 426:              {
 427:                  builder.Append(pMillisecond.ToString());
 428:                  builder.Append("秒");
 429:              }
 430:              return builder.ToString();
 431:          }
 432:          #endregion
 433:   
 434:          #region GetTwoDateSeconds 获得d1和d2之间的时间秒数
 435:          /// <summary>
 436:          /// 获得d1和d2之间的时间秒数
 437:          /// </summary>
 438:          /// <param name="d1">时间前</param>
 439:          /// <param name="d2">时间后</param>
 440:          /// <returns>返回d1和d2之间的时间秒数</returns>
 441:          public static long GetTwoDateSeconds(DateTime d1, DateTime d2)
 442:          {
 443:              TimeSpan ts = d2.Subtract(d1).Duration();
 444:              return Convert.ToInt64(ts.TotalSeconds);
 445:          }
 446:          #endregion
 447:   
 448:          
 449:   
 450:      }
 451:  }
<style type="text/css">.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } </style>