using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ZB.QueueSys.Common { public class DateTimeHelper { //定义一个用于保存静态变量的实例 private static DateTimeHelper instance = null; //定义一个保证线程同步的标识 private static readonly object locker = new object(); //构造函数为私有,使外界不能创建该类的实例 private DateTimeHelper() { } public static DateTimeHelper Instance { get { if (instance == null) { lock (locker) { if (instance == null) instance = new DateTimeHelper(); } } return instance; } } /// <summary> /// 计算两个时间之差,获取分钟数 /// </summary> /// <param name="one">减数</param> /// <param name="two">被减数</param> /// <returns></returns> public double GetTotalMinutesByTime(DateTime one, DateTime two) { DateTime now = DateTime.Now; TimeSpan times = one.Subtract(now); double minutes = times.TotalMinutes; return minutes; } /// <summary> /// 计算当前时间 - 入参时间(获取分钟数;) /// </summary> /// <param name="time">被减数</param> /// <returns></returns> public double GetTotalMinutesByTime(DateTime time) { DateTime now = DateTime.Now; TimeSpan times = now.Subtract(time); double minutes = times.TotalMinutes; return minutes; } /// <summary> /// 根据日期返回 星期(返回结果为英文) /// </summary> /// <param name="date">日期</param> /// <returns>星期</returns> public string ConvertDateToENWeek(DateTime date) { DayOfWeek dt = new DateTime(date.Year, date.Month, date.Day).DayOfWeek; return dt.ToString(); } /// <summary> /// 根据日期返回 星期(返回结果为中文) /// </summary> /// <param name="date">日期</param> /// <returns>星期</returns> public string ConvertDateToZHWeek(DateTime date) { string week = string.Empty; if (string.IsNullOrEmpty(date.ToString())) return week; DayOfWeek weekstr = new DateTime(date.Year, date.Month, date.Day).DayOfWeek; switch (weekstr.ToString()) { case "Monday": week = "星期一"; break; case "Tuesday": week = "星期二"; break; case "Wednesday": week = "星期三"; break; case "Thursday": week = "星期四"; break; case "Friday": week = "星期五"; break; case "Saturday": week = "星期六"; break; case "Sunday": week = "星期天"; break; } return week; } /// <summary> /// 得到本周第一天(星期一为第一天) /// </summary> /// <param name="date"></param> /// <returns></returns> public DateTime GetWeekFirstDay(DateTime date) { int weeknow = Convert.ToInt32(date.DayOfWeek);//得到结果是以周日为本周第一天 weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1)); int daydiff = (-1) * weeknow; string FirstDay = date.AddDays(daydiff).ToString("yyyy-MM-dd") + " 00:00:00"; return Convert.ToDateTime(FirstDay); } /// <summary> /// 得到本周最后一天(以星期天为最后一天) /// </summary> /// <param name="datetime"></param> /// <returns></returns> public DateTime GetWeekLastDay(DateTime date) { int weeknow = Convert.ToInt32(date.DayOfWeek);//得到结果是以周日为本周第一天 weeknow = (weeknow == 0 ? 7 : weeknow); int daydiff = (7 - weeknow); string LastDay = date.AddDays(daydiff).ToString("yyyy-MM-dd") + " 23:59:59"; return Convert.ToDateTime(LastDay); } /// <summary> /// 计算本周的周一日期 /// </summary> /// <returns></returns> public DateTime GetMondayDate() { return GetMondayDate(DateTime.Now); } /// <summary> /// 计算本周周日的日期 /// </summary> /// <returns></returns> private DateTime GetSundayDate() { return GetSundayDate(DateTime.Now); } /// <summary> /// 计算某日起始日期(礼拜一的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜一日期,后面的具体时、分、秒和传入值相等</returns> public DateTime GetMondayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } /// <summary> /// 计算某日结束日期(礼拜日的日期) /// </summary> /// <param name="someDate">该周中任意一天</param> /// <returns>返回礼拜日日期,后面的具体时、分、秒和传入值相等</returns> public DateTime GetSundayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Sunday; if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Add(ts); } public string GetTimeLength(DateTime beginTime, DateTime endTime) { try { string timeLength = string.Empty; TimeSpan begin = new TimeSpan(beginTime.Ticks); TimeSpan end = new TimeSpan(endTime.Ticks); TimeSpan ts = begin.Subtract(end).Duration(); timeLength = Convert.ToString(ts.Hours * 60 + ts.Minutes); return timeLength; } catch { } return string.Empty; } public bool IsToday(string date) { //入参 DateTime dt = DateTime.Parse(date); //当前日期 DateTime today = DateTime.Today; DateTime tempToday = new DateTime(dt.Year, dt.Month, dt.Day); if (today != tempToday) return false; return true; //if (input.Date != DateTime.Now.Date) //{ // //MessageBox.Show("不是当天"); // return false; //} //return true; } public bool IsToday(DateTime dt) { DateTime today = DateTime.Today; DateTime tempToday = new DateTime(dt.Year, dt.Month, dt.Day); if (today != tempToday) return false; return true; } } }
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!