using System; using System.Globalization; namespace Masuit.Tools.DateTimeExt { /// /// 时间相关操作帮助类 /// public static class TimeHelper { #region 返回每月的第一天和最后一天 /// /// 返回每月的第一天和最后一天 /// /// /// 月份 /// 第一天 /// 最后一天 public static void ReturnDateFormat(this DateTime _, int month, out string firstDay, out string lastDay) { int year = DateTime.Now.Year + month / 12; if (month != 12) { 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"); lastDay = DateTime.IsLeapYear(DateTime.Now.Year) ? DateTime.Now.ToString(year + "-0" + month + "-29") : 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; } } #endregion #region 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 /// /// 将时间格式化成 年月日 的形式,如果时间为null,返回当前系统时间 /// /// 年月日分隔符 /// 分隔符 /// xxxx年xx月xx日 public static string GetFormatDate(this DateTime dt, char separator) { if (dt.Equals(DBNull.Value)) { return GetFormatDate(DateTime.Now, separator); } string tem = $"yyyy{separator}MM{separator}dd"; return dt.ToString(tem); } #endregion #region 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 /// /// 将时间格式化成 时分秒 的形式,如果时间为null,返回当前系统时间 /// /// 当前日期时间对象 /// 分隔符 /// xx时xx分xx秒 public static string GetFormatTime(this DateTime dt, char separator) { if (dt.Equals(DBNull.Value)) { return GetFormatDate(DateTime.Now, separator); } string tem = $"hh{separator}mm{separator}ss"; return dt.ToString(tem); } #endregion #region 把秒转换成分钟 /// /// 把秒转换成分钟 /// /// /// 秒数 /// 分钟数 public static int SecondToMinute(this DateTime _, int second) { decimal mm = second / (decimal)60; return Convert.ToInt32(Math.Ceiling(mm)); } #endregion #region 返回某年某月最后一天 /// /// 返回某年某月最后一天 /// /// /// 年份 /// 月份 /// public static int GetMonthLastDate(this DateTime _, int year, int month) { DateTime lastDay = new DateTime(year, month, new GregorianCalendar().GetDaysInMonth(year, month)); int day = lastDay.Day; return day; } #endregion #region 获得两个日期的间隔 /// /// 获得两个日期的间隔 /// /// 日期一。 /// 日期二。 /// 日期间隔TimeSpan。 /// The return value is less than or greater than . public static TimeSpan DateDiff2(this DateTime dateTime1, DateTime dateTime2) { return dateTime1 - dateTime2; } #endregion #region 得到随机日期 /// /// 得到随机日期 /// /// 起始日期 /// 结束日期 /// 间隔日期之间的 随机日期 public static DateTime GetRandomTime(this DateTime time1, DateTime time2) { var random = new Random(); DateTime minTime; var ts = new TimeSpan(time1.Ticks - time2.Ticks); // 获取两个时间相隔的秒数 double dTotalSecontds = ts.TotalSeconds; int iTotalSecontds; if (dTotalSecontds > int.MaxValue) iTotalSecontds = int.MaxValue; else if (dTotalSecontds < int.MinValue) iTotalSecontds = int.MinValue; else iTotalSecontds = (int)dTotalSecontds; if (iTotalSecontds > 0) { minTime = time2; } else if (iTotalSecontds < 0) { minTime = time1; } else { return time1; } int maxValue = iTotalSecontds; if (iTotalSecontds <= int.MinValue) { maxValue = int.MinValue + 1; } int i = random.Next(Math.Abs(maxValue)); return minTime.AddSeconds(i); } #endregion #region Rss日期时间转换,将时间全部转换为GMT时间 /// /// Rss日期时间转换,将时间全部转换为GMT时间 /// /// Rss中读取的时间 /// 处理后的标准时间格式 public static string DateConvert(this string strDateTime) { strDateTime = strDateTime.Replace("+0000", "GMT"); strDateTime = strDateTime.Replace("+0100", "GMT"); strDateTime = strDateTime.Replace("+0200", "GMT"); strDateTime = strDateTime.Replace("+0300", "GMT"); strDateTime = strDateTime.Replace("+0400", "GMT"); strDateTime = strDateTime.Replace("+0500", "GMT"); strDateTime = strDateTime.Replace("+0600", "GMT"); strDateTime = strDateTime.Replace("+0700", "GMT"); strDateTime = strDateTime.Replace("+0800", "GMT"); strDateTime = strDateTime.Replace("-0000", "GMT"); strDateTime = strDateTime.Replace("-0100", "GMT"); strDateTime = strDateTime.Replace("-0200", "GMT"); strDateTime = strDateTime.Replace("-0300", "GMT"); strDateTime = strDateTime.Replace("-0400", "GMT"); strDateTime = strDateTime.Replace("-0500", "GMT"); strDateTime = strDateTime.Replace("-0600", "GMT"); strDateTime = strDateTime.Replace("-0700", "GMT"); strDateTime = strDateTime.Replace("-0800", "GMT"); DateTime dt = DateTime.Parse(strDateTime, null, DateTimeStyles.AdjustToUniversal); return dt.ToString(); } #endregion #region 时间相关操作类 /// /// 获得一段时间内有多少小时 /// /// 起始时间 /// 终止时间 /// 小时差 public static string GetTimeDelay(this DateTime dtStar, DateTime dtEnd) { long lTicks = (dtEnd.Ticks - dtStar.Ticks) / 10000000; string sTemp = (lTicks / 3600).ToString().PadLeft(2, '0') + ":"; sTemp += (lTicks % 3600 / 60).ToString().PadLeft(2, '0') + ":"; sTemp += (lTicks % 3600 % 60).ToString().PadLeft(2, '0'); return sTemp; } /// /// 获得8位时间整型数字 /// /// 当前的日期时间对象 /// 8位时间整型数字 public static string GetDateString(this DateTime dt) { return dt.Year + dt.Month.ToString().PadLeft(2, '0') + dt.Day.ToString().PadLeft(2, '0'); } #endregion #region 返回时间差 /// /// 返回时间差 /// /// 时间1 /// 时间2 /// 时间差 public static string DateDiff(this DateTime dateTime1, DateTime dateTime2) { string dateDiff; var ts = dateTime2 - dateTime1; if (ts.Days >= 1) { dateDiff = dateTime1.Month + "月" + dateTime1.Day + "日"; } else { dateDiff = ts.Hours > 1 ? ts.Hours + "小时前" : ts.Minutes + "分钟前"; } return dateDiff; } /// /// 计算2个时间差 /// /// 开始时间 /// 结束时间 /// 时间差 public static string GetDiffTime(this DateTime beginTime, DateTime endTime) { string strResout = string.Empty; //获得2时间的时间间隔秒计算 TimeSpan span = endTime.Subtract(beginTime); int sec = Convert.ToInt32(span.TotalSeconds); int minutes = 1 * 60; int hours = minutes * 60; int day = hours * 24; int month = day * 30; int year = month * 12; //提醒时间,到了返回1,否则返回0 if (sec > year) { strResout += sec / year + "年"; sec %= year; //剩余 } if (sec > month) { strResout += sec / month + "月"; sec %= month; } if (sec > day) { strResout += sec / day + "天"; sec %= day; } if (sec > hours) { strResout += sec / hours + "小时"; sec %= hours; } if (sec > minutes) { strResout += sec / minutes + "分"; sec %= minutes; } strResout += sec + "秒"; return strResout; } #endregion #region 时间其他转换静态方法 /// /// C#的时间到Javascript的时间的转换 /// /// C#的时间 /// Javascript的时间 public static long CsharpTime2JavascriptTime(this DateTime theDate) { DateTime d1 = new DateTime(1970, 1, 1); DateTime d2 = theDate.ToUniversalTime(); TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks); return (long)ts.TotalMilliseconds; } /// /// PHP的时间转换成C#中的DateTime /// /// /// php的时间 /// C#的时间 public static DateTime PhpTime2CsharpTime(this DateTime _, long time) { DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳 long t = (time + 8 * 60 * 60) * 10000000 + timeStamp.Ticks; return new DateTime(t); } /// /// C#中的DateTime转换成PHP的时间 /// /// C#时间 /// php时间 public static long CsharpTime2PhpTime(this DateTime time) { DateTime timeStamp = new DateTime(1970, 1, 1); //得到1970年的时间戳 //注意这里有时区问题,用now就要减掉8个小时 return (DateTime.UtcNow.Ticks - timeStamp.Ticks) / 10000000; } #endregion } }