一、引用
1 using System;
二、使用
1、日期帮助类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #region 获得时间戳 2 /// <summary> 3 /// 获得时间戳 4 /// </summary> 5 /// <returns></returns> 6 public static string GetTimeStamp() 7 { 8 TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); 9 return Convert.ToInt64(ts.TotalMilliseconds).ToString(); 10 } 11 #endregion 12 13 #region 时间转提示文本 14 /// <summary> 15 /// 时间转提示文本 16 /// </summary> 17 /// <param name="days">天</param> 18 /// <param name="hours">小时</param> 19 /// <param name="minutes">分钟</param> 20 /// <param name="seconds">秒</param> 21 /// <param name="IsMilliseconds">是否获取毫秒级</param> 22 /// <returns></returns> 23 public static string TimeToTooltip(int days, int hours, int minutes, int seconds, bool IsMilliseconds) 24 { 25 TimeSpan ts = new TimeSpan(days, hours, minutes, seconds); 26 var tooltip = ""; 27 if (ts.Days > 0) 28 { 29 tooltip += ts.Days + "天"; 30 } 31 if (ts.Hours > 0) 32 { 33 tooltip += ts.Hours + "小时"; 34 } 35 if (ts.Minutes > 0) 36 { 37 tooltip += ts.Minutes + "分钟"; 38 } 39 if (ts.Seconds > 0) 40 { 41 tooltip += ts.Seconds + "秒"; 42 } 43 if (IsMilliseconds && ts.Milliseconds > 0) 44 { 45 tooltip += ts.Milliseconds + "毫秒"; 46 } 47 return tooltip; 48 } 49 #endregion 50 51 #region 把秒转换成分钟 52 /// <summary> 53 /// 把秒转换成分钟 54 /// </summary> 55 /// <returns></returns> 56 public static int SecondToMinute(int Second) 57 { 58 decimal mm = (decimal)((decimal)Second / (decimal)60); 59 return Convert.ToInt32(Math.Ceiling(mm)); 60 } 61 #endregion 62 63 #region 返回时间差 64 /// <summary> 65 /// 返回时间差 66 /// </summary> 67 /// <param name="DateTime1"></param> 68 /// <param name="DateTime2"></param> 69 /// <returns></returns> 70 public static string DateDiff(DateTime DateTime1, DateTime DateTime2) 71 { 72 string dateDiff = null; 73 try 74 { 75 //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 76 //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 77 //TimeSpan ts = ts1.Subtract(ts2).Duration(); 78 TimeSpan ts = DateTime2 - DateTime1; 79 if (ts.Days >= 1) 80 { 81 dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日"; 82 } 83 else 84 { 85 if (ts.Hours > 1) 86 { 87 dateDiff = ts.Hours.ToString() + "小时前"; 88 } 89 else 90 { 91 dateDiff = ts.Minutes.ToString() + "分钟前"; 92 } 93 } 94 } 95 catch 96 { } 97 return dateDiff; 98 } 99 #endregion 100 101 #region 获得两个日期的间隔 102 /// <summary> 103 /// 获得两个日期的间隔 104 /// </summary> 105 /// <param name="DateTime1">日期一。</param> 106 /// <param name="DateTime2">日期二。</param> 107 /// <returns>日期间隔TimeSpan。</returns> 108 public static TimeSpan DateDiff2(DateTime DateTime1, DateTime DateTime2) 109 { 110 TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); 111 TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); 112 TimeSpan ts = ts1.Subtract(ts2).Duration(); 113 return ts; 114 } 115 #endregion 116 117 #region 格式化日期时间 118 /// <summary> 119 /// 格式化日期时间 120 /// </summary> 121 /// <param name="dateTime1">日期时间</param> 122 /// <param name="dateMode">显示模式</param> 123 /// <returns>0-9种模式的日期</returns> 124 public static string FormatDate(DateTime dateTime1, string dateMode) 125 { 126 switch (dateMode) 127 { 128 case "0": 129 return dateTime1.ToString("yyyy-MM-dd"); 130 case "1": 131 return dateTime1.ToString("yyyy-MM-dd HH:mm:ss"); 132 case "2": 133 return dateTime1.ToString("yyyy/MM/dd"); 134 case "3": 135 return dateTime1.ToString("yyyy年MM月dd日"); 136 case "4": 137 return dateTime1.ToString("MM-dd"); 138 case "5": 139 return dateTime1.ToString("MM/dd"); 140 case "6": 141 return dateTime1.ToString("MM月dd日"); 142 case "7": 143 return dateTime1.ToString("yyyy-MM"); 144 case "8": 145 return dateTime1.ToString("yyyy/MM"); 146 case "9": 147 return dateTime1.ToString("yyyy年MM月"); 148 default: 149 return dateTime1.ToString(); 150 } 151 } 152 #endregion 153 154 #region 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 155 /// <summary> 156 /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 157 /// </summary> 158 /// <param name="dt">年月日分隔符</param> 159 /// <param name="Separator"></param> 160 /// <returns></returns> 161 public static string GetFormatDate(DateTime dt, char Separator) 162 { 163 if (dt != null && !dt.Equals(DBNull.Value)) 164 { 165 string tem = string.Format("yyyy{0}MM{1}dd", Separator, Separator); 166 return dt.ToString(tem); 167 } 168 else 169 { 170 return GetFormatDate(DateTime.Now, Separator); 171 } 172 } 173 #endregion 174 175 #region 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 176 /// <summary> 177 /// 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 178 /// </summary> 179 /// <param name="dt"></param> 180 /// <param name="Separator"></param> 181 /// <returns></returns> 182 public static string GetFormatTime(DateTime dt, char Separator) 183 { 184 if (dt != null && !dt.Equals(DBNull.Value)) 185 { 186 string tem = string.Format("hh{0}mm{1}ss", Separator, Separator); 187 return dt.ToString(tem); 188 } 189 else 190 { 191 return GetFormatDate(DateTime.Now, Separator); 192 } 193 } 194 #endregion 195 196 #region 得到随机日期 197 /// <summary> 198 /// 得到随机日期 199 /// </summary> 200 /// <param name="time1">起始日期</param> 201 /// <param name="time2">结束日期</param> 202 /// <returns>间隔日期之间的 随机日期</returns> 203 public static DateTime GetRandomTime(DateTime time1, DateTime time2) 204 { 205 Random random = new Random(); 206 DateTime minTime = new DateTime(); 207 DateTime maxTime = new DateTime(); 208 209 System.TimeSpan ts = new System.TimeSpan(time1.Ticks - time2.Ticks); 210 211 // 获取两个时间相隔的秒数 212 double dTotalSecontds = ts.TotalSeconds; 213 int iTotalSecontds = 0; 214 215 if (dTotalSecontds > System.Int32.MaxValue) 216 { 217 iTotalSecontds = System.Int32.MaxValue; 218 } 219 else if (dTotalSecontds < System.Int32.MinValue) 220 { 221 iTotalSecontds = System.Int32.MinValue; 222 } 223 else 224 { 225 iTotalSecontds = (int)dTotalSecontds; 226 } 227 228 229 if (iTotalSecontds > 0) 230 { 231 minTime = time2; 232 maxTime = time1; 233 } 234 else if (iTotalSecontds < 0) 235 { 236 minTime = time1; 237 maxTime = time2; 238 } 239 else 240 { 241 return time1; 242 } 243 244 int maxValue = iTotalSecontds; 245 246 if (iTotalSecontds <= System.Int32.MinValue) 247 maxValue = System.Int32.MinValue + 1; 248 249 int i = random.Next(System.Math.Abs(maxValue)); 250 251 return minTime.AddSeconds(i); 252 } 253 #endregion 254 255 #region 返回某年某月最后一天 256 /// <summary> 257 /// 返回某年某月最后一天 258 /// </summary> 259 /// <param name="year">年份</param> 260 /// <param name="month">月份</param> 261 /// <returns>日</returns> 262 public static int GetMonthLastDate(int year, int month) 263 { 264 DateTime lastDay = new DateTime(year, month, new System.Globalization.GregorianCalendar().GetDaysInMonth(year, month)); 265 int Day = lastDay.Day; 266 return Day; 267 } 268 #endregion 269 270 #region 返回某月的第一天和最后一天 271 /// <summary> 272 /// 返回某月的第一天和最后一天 273 /// </summary> 274 /// <param name="month"></param> 275 /// <param name="firstDay"></param> 276 /// <param name="lastDay"></param> 277 public static void ReturnDateFormat(int month, out string firstDay, out string lastDay) 278 { 279 int year = DateTime.Now.Year + month / 12; 280 if (month != 12) 281 { 282 month = month % 12; 283 } 284 switch (month) 285 { 286 case 1: 287 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 288 lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); 289 break; 290 case 2: 291 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 292 if (DateTime.IsLeapYear(DateTime.Now.Year)) 293 lastDay = DateTime.Now.ToString(year + "-0" + month + "-29"); 294 else 295 lastDay = DateTime.Now.ToString(year + "-0" + month + "-28"); 296 break; 297 case 3: 298 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 299 lastDay = DateTime.Now.ToString("yyyy-0" + month + "-31"); 300 break; 301 case 4: 302 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 303 lastDay = DateTime.Now.ToString(year + "-0" + month + "-30"); 304 break; 305 case 5: 306 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 307 lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); 308 break; 309 case 6: 310 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 311 lastDay = DateTime.Now.ToString(year + "-0" + month + "-30"); 312 break; 313 case 7: 314 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 315 lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); 316 break; 317 case 8: 318 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 319 lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); 320 break; 321 case 9: 322 firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); 323 lastDay = DateTime.Now.ToString(year + "-0" + month + "-30"); 324 break; 325 case 10: 326 firstDay = DateTime.Now.ToString(year + "-" + month + "-01"); 327 lastDay = DateTime.Now.ToString(year + "-" + month + "-31"); 328 break; 329 case 11: 330 firstDay = DateTime.Now.ToString(year + "-" + month + "-01"); 331 lastDay = DateTime.Now.ToString(year + "-" + month + "-30"); 332 break; 333 default: 334 firstDay = DateTime.Now.ToString(year + "-" + month + "-01"); 335 lastDay = DateTime.Now.ToString(year + "-" + month + "-31"); 336 break; 337 } 338 } 339 #endregion 340 341 #region 返回当前日期的最后一秒时间 342 /// <summary> 343 /// 返回当前日期的23:59:59 344 /// </summary> 345 /// <param name="time"></param> 346 public static DateTime ReturnDayEnd(DateTime time) 347 { 348 string dayTime = time.ToString("yyy-MM-dd"); 349 return dayTime.ToDateTime().AddDays(1).AddSeconds(-1); 350 } 351 #endregion 352 353 #region 返回当前日期的第一秒时间 354 /// <summary> 355 /// 返回当前日期的00:00:00 356 /// </summary> 357 /// <param name="time"></param> 358 public static DateTime ReturnDayStart(DateTime time) 359 { 360 string dayTime = time.ToString("yyy-MM-dd"); 361 return dayTime.ToDateTime(); 362 } 363 #endregion