using System; using System.Diagnostics; using System.Globalization; namespace DG.Tool { /// /// 时间类 /// 1、SecondToMinute(int Second) 把秒转换成分钟 /// public class TimeHelper { #region Stopwatch计时器 /// /// 计时器开始 /// /// public static Stopwatch TimerStart() { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); return watch; } /// /// 计时器结束 /// /// /// public static string TimerEnd(Stopwatch watch) { watch.Stop(); double costtime = watch.ElapsedMilliseconds; return costtime.ToString(); } #endregion Stopwatch计时器 //返回每月的第一天和最后一天 public static void ReturnDateFormat(int month, out string firstDay, out string lastDay) { int year = DateTime.Now.Year + month / 12; if (month != 12) { month = month % 12; } switch (month) { case 1: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); break; case 2: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); if (DateTime.IsLeapYear(DateTime.Now.Year)) lastDay = DateTime.Now.ToString(year + "-0" + month + "-29"); else lastDay = DateTime.Now.ToString(year + "-0" + month + "-28"); break; case 3: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString("yyyy-0" + month + "-31"); break; case 4: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-30"); break; case 5: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); break; case 6: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-30"); break; case 7: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); break; case 8: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-31"); break; case 9: firstDay = DateTime.Now.ToString(year + "-0" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-0" + month + "-30"); break; case 10: firstDay = DateTime.Now.ToString(year + "-" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-" + month + "-31"); break; case 11: firstDay = DateTime.Now.ToString(year + "-" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-" + month + "-30"); break; default: firstDay = DateTime.Now.ToString(year + "-" + month + "-01"); lastDay = DateTime.Now.ToString(year + "-" + month + "-31"); break; } } /// /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 /// /// 年月日分隔符 /// /// public string GetFormatDate(DateTime dt, char Separator) { if (dt != null && !dt.Equals(DBNull.Value)) { string tem = string.Format("yyyy{0}MM{1}dd", Separator, Separator); return dt.ToString(tem); } else { return GetFormatDate(DateTime.Now, Separator); } } /// /// 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 /// /// /// /// public string GetFormatTime(DateTime dt, char Separator) { if (dt != null && !dt.Equals(DBNull.Value)) { string tem = string.Format("hh{0}mm{1}ss", Separator, Separator); return dt.ToString(tem); } else { return GetFormatDate(DateTime.Now, Separator); } } /// /// 把秒转换成分钟 /// /// public static int SecondToMinute(int Second) { decimal mm = (decimal)((decimal)Second / (decimal)60); return Convert.ToInt32(Math.Ceiling(mm)); } #region 返回某年某月最后一天 /// /// 返回某年某月最后一天 /// /// 年份 /// 月份 /// public static int GetMonthLastDate(int year, int month) { DateTime lastDay = new DateTime(year, month, new GregorianCalendar().GetDaysInMonth(year, month)); int Day = lastDay.Day; return Day; } #endregion 返回某年某月最后一天 #region 返回时间差 public static string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; try { //TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); //TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); //TimeSpan ts = ts1.Subtract(ts2).Duration(); TimeSpan ts = DateTime2 - DateTime1; if (ts.Days >= 1) { dateDiff = DateTime1.Month.ToString() + "月" + DateTime1.Day.ToString() + "日"; } else { if (ts.Hours > 1) { dateDiff = ts.Hours.ToString() + "小时前"; } else { dateDiff = ts.Minutes.ToString() + "分钟前"; } } } catch { } return dateDiff; } #endregion 返回时间差 #region 获得两个日期的间隔 /// /// 获得两个日期的间隔 /// /// 日期一。 /// 日期二。 /// 日期间隔TimeSpan。 public static TimeSpan DateDiff2(DateTime DateTime1, DateTime DateTime2) { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); return ts; } #endregion 获得两个日期的间隔 #region 格式化日期时间 /// /// 格式化日期时间 /// /// 日期时间 /// 显示模式 /// 0-9种模式的日期 public static string FormatDate(DateTime dateTime1, string dateMode) { switch (dateMode) { case "0": return dateTime1.ToString("yyyy-MM-dd"); case "1": return dateTime1.ToString("yyyy-MM-dd HH:mm:ss"); case "2": return dateTime1.ToString("yyyy/MM/dd"); case "3": return dateTime1.ToString("yyyy年MM月dd日"); case "4": return dateTime1.ToString("MM-dd"); case "5": return dateTime1.ToString("MM/dd"); case "6": return dateTime1.ToString("MM月dd日"); case "7": return dateTime1.ToString("yyyy-MM"); case "8": return dateTime1.ToString("yyyy/MM"); case "9": return dateTime1.ToString("yyyy年MM月"); default: return dateTime1.ToString(); } } #endregion 格式化日期时间 #region 得到随机日期 /// /// 得到随机日期 /// /// 起始日期 /// 结束日期 /// 间隔日期之间的 随机日期 public static DateTime GetRandomTime(DateTime time1, DateTime time2) { Random random = new Random(); DateTime minTime = new DateTime(); DateTime maxTime = new DateTime(); TimeSpan ts = new TimeSpan(time1.Ticks - time2.Ticks); // 获取两个时间相隔的秒数 double dTotalSecontds = ts.TotalSeconds; int iTotalSecontds = 0; if (dTotalSecontds > Int32.MaxValue) { iTotalSecontds = Int32.MaxValue; } else if (dTotalSecontds < Int32.MinValue) { iTotalSecontds = Int32.MinValue; } else { iTotalSecontds = (int)dTotalSecontds; } if (iTotalSecontds > 0) { minTime = time2; maxTime = time1; } else if (iTotalSecontds < 0) { minTime = time1; maxTime = time2; } else { return time1; } int maxValue = iTotalSecontds; if (iTotalSecontds <= Int32.MinValue) maxValue = Int32.MinValue + 1; int i = random.Next(Math.Abs(maxValue)); return minTime.AddSeconds(i); } #endregion 得到随机日期 /// /// DateTime时间格式转换为Unix时间戳格式 /// /// /// public static int ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } /// /// 时间戳转为C#格式时间 /// /// /// public static DateTime GetTimeFromLinuxTime(long timeStamp) { System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0); dtDateTime = dtDateTime.AddMilliseconds(timeStamp).ToLocalTime(); return dtDateTime; } } }