首页 > 代码库 > 日期类2

日期类2

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace AkDTH.Common
   7:  {
   8:      /// <summary>
   9:      /// 时间字符串帮助类
  10:      /// author:Andrew.He
  11:      /// 生成时间格式:
  12:      /// 默认返回完整格式 yyyy-MM-dd HH:mm:ss
  13:      /// YY      返回年月日时分秒    yyyy-MM-dd HH:mm:ss
  14:      /// HH      返回年月日时分秒    yyyy-MM-dd HH:mm:ss
  15:      /// hh      返回年月日时分秒    yyyy-MM-dd hh:mm:ss
  16:      /// yM      返回年月            yyyy-MM
  17:      /// Md      返回月日            MM-dd
  18:      /// yMd     返回年月日          yyyy-MM-dd
  19:      /// yHm     返回年月日时分      yyyy-MM-dd HH:mm   
  20:      /// yhm     返回年月日时分      yyyy-MM-dd hh:mm
  21:      /// Hms     返回时分秒          HH:mm:ss
  22:      /// hms     返回时分秒          hh:mm:ss
  23:      /// Hm      返回时分            HH:mm
  24:      /// hm      返回时分            hh:mm
  25:      /// y:yyyy  返回年              yyyy   
  26:      /// M:MM    返回月              MM
  27:      /// d:dd    返回日              dd
  28:      /// h:hh    返回时              hh   
  29:      /// H:HH    返回时              HH
  30:      /// m:mm    返回分              mm
  31:      /// s:ss    返回秒              ss   
  32:      /// </summary>
  33:      public class DateTimeHelper
  34:      {
  35:   
  36:          #region GetDateTimeString 返回当前时间的格式化字符串
  37:          /// <summary>
  38:          /// 返回当前时间的格式化字符串
  39:          /// </summary>
  40:          /// <param name="format">格式化字符串类型</param>
  41:          /// <param name="split1">年月日连接字符串</param>
  42:          /// <param name="split2">时分秒连接字符串</param>
  43:          /// <returns></returns>
  44:          public static string GetDateTimeString(TimeFormat format =TimeFormat.other, string split1 = "-", string split2 = ":")
  45:          {
  46:              return GetDateTimeString(DateTime.Now,format,split1,split2);
  47:          }
  48:          #endregion
  49:   
  50:          #region GetDateTimeString 返回时间的格式化字符串
  51:          /// <summary>
  52:          /// 返回时间的格式化字符串
  53:          /// </summary>
  54:          /// <param name="dt">要格式化的时间</param>
  55:          /// <param name="format">格式化字符串类型</param>
  56:          /// <param name="split1">年月日连接字符串</param>
  57:          /// <param name="split2">时分秒连接字符串</param>
  58:          /// <returns></returns>
  59:          public static string GetDateTimeString(DateTime dt, TimeFormat format = TimeFormat.other, string split1 = "-", string split2 = ":")
  60:          {
  61:              string toString = "yyyy-MM-dd HH:mm:ss";
  62:              switch (format)
  63:              { 
  64:                  case TimeFormat.other:
  65:                  case TimeFormat.YY:
  66:                  case TimeFormat.HH:
  67:                      break;
  68:                  case TimeFormat.hh:
  69:                      toString="yyyy-MM-dd hh:mm:ss";
  70:                      break;
  71:                  case TimeFormat.yM:
  72:                      toString="yyyy-MM";
  73:                      break;
  74:                  case TimeFormat.Md: 
  75:                      toString = "MM-dd";
  76:                      break;
  77:                  case TimeFormat.yMd: 
  78:                      toString = "yyyy-MM-dd";
  79:                      break;
  80:                  case TimeFormat.yHm: 
  81:                      toString = "yyyy-MM-dd HH:mm";
  82:                      break;
  83:                  case TimeFormat.yhm:
  84:                      toString = "yyyy-MM-dd hh:mm";
  85:                      break;
  86:                  case TimeFormat.Hms: 
  87:                      toString = "HH:mm:ss";
  88:                      break;
  89:                  case TimeFormat.hms: 
  90:                      toString = "hh:mm:ss";
  91:                      break;
  92:                  case TimeFormat.Hm: 
  93:                      toString = "HH:mm";
  94:                      break;
  95:                  case TimeFormat.hm: 
  96:                      toString = "hh:mm";
  97:                      break;
  98:                  case TimeFormat.y: 
  99:                      toString = "yyyy";
 100:                      break;
 101:                  case TimeFormat.M: 
 102:                      toString = "MM";
 103:                      break;
 104:                  case TimeFormat.d: 
 105:                      toString = "dd";
 106:                      break;
 107:                  case TimeFormat.H: 
 108:                      toString = "HH";
 109:                      break;
 110:                  case TimeFormat.h: 
 111:                      toString = "hh";
 112:                      break;
 113:                  case TimeFormat.m: 
 114:                      toString = "mm";
 115:                      break;
 116:                  case TimeFormat.s: 
 117:                      toString = "ss";
 118:                      break;
 119:              }
 120:              toString = toString.Replace("-", split1);
 121:              toString = toString.Replace(":", split2);
 122:   
 123:              return dt.ToString(toString);
 124:          }
 125:          #endregion
 126:   
 127:          #region GetDateTimeString 返回格式化的时间字符串
 128:          /// <summary>
 129:          /// 返回格式化的时间字符串
 130:          /// </summary>
 131:          /// <param name="dt">待格式化的时间</param>
 132:          /// <param name="format">格式化字符串类型</param>
 133:          /// <param name="split1">年月日连接字符串</param>
 134:          /// <param name="split2">时分秒连接字符串</param>
 135:          /// <returns></returns>
 136:          public static string GetDateTimeString(object dt, TimeFormat format = TimeFormat.other, string split1 = "-", string split2 = ":")
 137:          {
 138:              return GetDateTimeString(Convert.ToDateTime(dt), format, split1, split2);
 139:          }
 140:          #endregion
 141:   
 142:          #region GetDateTime 获取基准时间
 143:          /// <summary>
 144:          /// 获取基准时间
 145:          /// </summary>
 146:          /// <returns></returns>
 147:          public static DateTime GetDateTime()
 148:          {
 149:              return GetDateTime(1900);
 150:          }
 151:          #endregion
 152:   
 153:          #region GetDateTime 获取给定的时间
 154:          /// <summary>
 155:          /// 获取给定的时间
 156:          /// </summary>
 157:          /// <param name="year"></param>
 158:          /// <param name="month"></param>
 159:          /// <param name="day"></param>
 160:          /// <param name="hour"></param>
 161:          /// <param name="minite"></param>
 162:          /// <param name="second"></param>
 163:          /// <returns></returns>
 164:          public static DateTime GetDateTime(int year, int month=1, int day=1, int hour=0, int minite=0, int second=0)
 165:          {
 166:              return new DateTime(year, month, day, hour, minite, second);
 167:          }
 168:          #endregion
 169:   
 170:          #region GetDateTimeStr 获取基准时间的给定格式的时间字符串
 171:          /// <summary>
 172:          /// 获取基准时间的给定格式的时间字符串
 173:          /// </summary>
 174:          /// <param name="format"></param>
 175:          /// <param name="split1"></param>
 176:          /// <param name="split2"></param>
 177:          /// <returns></returns>
 178:          public static string GetDateTimeStr(TimeFormat format = TimeFormat.other, string split1 = "-", string split2 = ":")
 179:          {
 180:              return GetDateTimeStr(1900,1,1,0,0,0,format,split1,split2);
 181:          }
 182:          #endregion
 183:   
 184:          #region GetDateTimeStr 获取给定时间的时间字符串
 185:          /// <summary>
 186:          /// 获取给定时间的时间字符串
 187:          /// </summary>
 188:          /// <param name="year"></param>
 189:          /// <param name="month"></param>
 190:          /// <param name="day"></param>
 191:          /// <param name="hour"></param>
 192:          /// <param name="minite"></param>
 193:          /// <param name="second"></param>
 194:          /// <param name="format"></param>
 195:          /// <param name="split1"></param>
 196:          /// <param name="split2"></param>
 197:          /// <returns></returns>
 198:          public static string GetDateTimeStr(int year, int month=1, int day=1, int hour=0, int minite=0, int second=0,TimeFormat format = TimeFormat.other, string split1 = "-", string split2 = ":")
 199:          {
 200:              return GetDateTimeString(GetDateTime(year, month, day, hour, minite, second), format, split1, split2);
 201:          }
 202:          #endregion
 203:      }
 204:   
 205:      #region TimeFormat 格式时间字符串枚举
 206:      /// <summary>
 207:      /// 格式时间字符串枚举
 208:      /// </summary>
 209:      public enum TimeFormat
 210:      { 
 211:          /// <summary>
 212:          /// 返回年月日时分秒    yyyy-MM-dd HH:mm:ss
 213:          /// </summary>
 214:          YY,
 215:          /// <summary>
 216:          /// 返回年月日时分秒    yyyy-MM-dd HH:mm:ss
 217:          /// </summary>
 218:          HH,
 219:          /// <summary>
 220:          /// 返回年月日时分秒    yyyy-MM-dd hh:mm:ss
 221:          /// </summary>
 222:          hh,
 223:          /// <summary>
 224:          /// yM      返回年月            yyyy-MM
 225:          /// </summary>
 226:          yM,
 227:          /// <summary>
 228:          /// 返回月日            MM-dd
 229:          /// </summary>
 230:          Md,
 231:          /// <summary>
 232:          /// 返回年月日          yyyy-MM-dd
 233:          /// </summary>
 234:          yMd,
 235:          /// <summary>
 236:          /// 返回年月日时分      yyyy-MM-dd hh:mm
 237:          /// </summary>
 238:          yhm,
 239:          /// <summary>
 240:          /// 返回年月日时分      yyyy-MM-dd HH:mm
 241:          /// </summary>
 242:          yHm,
 243:          /// <summary>
 244:          /// 返回时分秒          HH:mm:ss
 245:          /// </summary>
 246:          Hms,
 247:          /// <summary>
 248:          /// 返回时分秒          hh:mm:ss
 249:          /// </summary>
 250:          hms,
 251:          /// <summary>
 252:          /// 返回时分            HH:mm
 253:          /// </summary>
 254:          Hm,
 255:          /// <summary>
 256:          /// 返回时分            hh:mm
 257:          /// </summary>
 258:          hm,
 259:          /// <summary>
 260:          /// 返回年              yyyy 
 261:          /// </summary>
 262:          y,
 263:          /// <summary>
 264:          /// 返回月              MM
 265:          /// </summary>
 266:          M,
 267:          /// <summary>
 268:          /// 返回日              dd
 269:          /// </summary>
 270:          d,
 271:          /// <summary>
 272:          /// 返回时              HH
 273:          /// </summary>
 274:          H,
 275:          /// <summary>
 276:          /// 返回时              hh
 277:          /// </summary>
 278:          h,
 279:          /// <summary>
 280:          /// 返回分              mm
 281:          /// </summary>
 282:          m,
 283:          /// <summary>
 284:          /// 返回秒              ss 
 285:          /// </summary>
 286:          s,
 287:          /// <summary>
 288:          /// 默认返回完整格式 yyyy-MM-dd HH:mm:ss
 289:          /// </summary>
 290:          other
 291:      }
 292:      #endregion
 293:   
 294:   
 295:  }
<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>