您好,登錄后才能下訂單哦!
在.net項目中,對DateTime的相關操作使用的比較多,例如時間格式的轉換,時間間隔的計算,時間所屬的區間計算等,在這些要求中,雖然使用起來較為的簡單,但是在轉換的過程中,較為容易出錯,花費的時間也較多,現在總結一些常用的時間操作方法,以便在項目開發中節省時間。
一下代碼中需要使用到如下的實體類:
/// <summary> /// 周(星期)信息實體類 /// </summary> [Serializable] public class WeekInfo { /// <summary> /// 周(星期)信息實體類 /// </summary> public WeekInfo() { Number = 0; BeginDate = DateTime.MinValue; EndDate = DateTime.MaxValue; } /// <summary> /// 周數 /// </summary> public int Number { get; set; } /// <summary> /// 開始時間 /// </summary> public DateTime BeginDate { get; set; } /// <summary> /// 結束時間 /// </summary> public DateTime EndDate { get; set; } /// <summary> /// 輸出第幾周和日期間隔 /// <para>默認格式:</para> /// <para>第幾周,從2012年12月21日至2112年12月21日。</para> /// </summary> /// <returns>第幾周和日期間隔</returns> public override string ToString() { return string.Format("第{0}周,從{1}至{2}。", Number, BeginDate.ToString("yyyy年MM月dd日"), EndDate.ToString("yyyy年MM月dd日")); } /// <summary> /// 輸出第幾周 /// </summary> /// <param name="isFill">少于2位時是否補零</param> /// <returns>第幾周</returns> public string GetWeekString(bool isFill) { string format = "第{0}周"; if (isFill && Number < 10) format = "第0{0}周"; return string.Format(format, Number); } /// <summary> /// 輸出日期間隔 /// </summary> /// <param name="inputString">輸出格式化字符串</param> /// <param name="dateString">日期格式化字符串</param> /// <returns>日期間隔</returns> public string GetDateString(string inputString, string dateString) { if (string.IsNullOrWhiteSpace(inputString) || string.IsNullOrWhiteSpace(dateString)) return null; try { return string.Format(inputString, BeginDate.ToString(dateString), EndDate.ToString(dateString)); } catch (Exception ex) { return ex.Message; } } }
1.獲得年度第一個周一的日期:
/// <summary> /// 獲得年度第一個周一的日期 /// </summary> /// <param name="year">需要計算的年份</param> /// <param name="jumpYear">年度第一周是否跳過跨年的周數</param> /// <param name="offset">年度第一個周一偏移量</param> /// <returns>年度第一個周一的日期</returns> public static DateTime WeekOfFirstDay(int year, bool jumpYear, out int offset) { var firstDate = YearOfFirstDay(year); //該年的第一天 var firstWeek = (int)firstDate.DayOfWeek; //該年的第一天是周幾 offset = 0; //周一偏移量 switch (firstWeek) { case 1: //星期一 offset = 0; break; case 2: //星期二 offset = jumpYear ? 6 : -1; break; case 3: //星期三 offset = jumpYear ? 5 : -2; break; case 4: //星期四 offset = jumpYear ? 4 : -3; break; case 5: //星期五 offset = jumpYear ? 3 : -4; break; case 6: //星期六 offset = jumpYear ? 2 : -5; break; case 0: //星期日 offset = jumpYear ? 1 : -6; break; } firstDate = firstDate.AddDays(offset); return firstDate; }
2.獲得年度周(星期)信息實體集合列表:
/// <summary> /// 獲得年度周(星期)信息實體集合列表 /// </summary> /// <param name="year">需要計算的年份</param> /// <param name="jumpYear">年度第一周是否跳過跨年的周數</param> /// <returns>周(星期)信息實體集合列表對象</returns> public static IList<WeekInfo> WeekOfList(int year, bool jumpYear) { IList<WeekInfo> weekList = new List<WeekInfo>(); if (year <= 0001 || year >= 9999) return weekList; var offset = 0; var firstDate = WeekOfFirstDay(year, jumpYear, out offset); //年度周一的日期 var index = 1; while (true) { if (index > 54) break; var weekInfo = new WeekInfo { Number = index, BeginDate = firstDate, EndDate = firstDate.AddDays(6) }; weekList.Add(weekInfo); firstDate = firstDate.AddDays(7); //下周 if (jumpYear) { if (firstDate.Year != year) break; } else { if (firstDate.AddDays(6).Year != year) break; } index++; } return weekList; }
3.獲得某年第某周的開始日期和結束日期:
/// <summary> /// 獲得某年第某周的開始日期和結束日期 /// </summary> /// <param name="year">需要計算的年份</param> /// <param name="weekNumber">需要計算的周數</param> /// <param name="jumpYear">年度第一周是否跳過跨年的周數</param> /// <param name="weekBeginDate">開始日期</param> /// <param name="weekEndDate">結束日期</param> public static void WeekOfDate(int year, int weekNumber, bool jumpYear, out DateTime weekBeginDate, out DateTime weekEndDate) { weekBeginDate = DateTime.MinValue; weekEndDate = DateTime.MaxValue; if (year <= 0001 || year >= 9999 || weekNumber < 1 || weekNumber > 54) return; int offset; var firstDate = WeekOfFirstDay(year, jumpYear, out offset); //年度周一的日期 firstDate = firstDate.AddDays((weekNumber - 1) * 7); weekBeginDate = firstDate; weekEndDate = firstDate.AddDays(6); }
4.獲得某個日期屬于某年的第幾周:
/// <summary> /// 獲得某個日期屬于某年的第幾周 /// </summary> /// <param name="currentDate">需要計算的日期</param> /// <param name="jumpYear">年度第一周是否跳過跨年的周數</param> /// <returns>某年的第幾周</returns> public static int WeekOfCurrent(DateTime currentDate, bool jumpYear) { if (DateTime.MinValue == currentDate || DateTime.MaxValue == currentDate) return 0; int result, offset; var firstDate = WeekOfFirstDay(currentDate.Year, jumpYear, out offset); //年度周一的日期 var firstWeek = (int)firstDate.DayOfWeek; //該年的第一天是周幾 var currentDay = currentDate.DayOfYear; if (offset > 0) { currentDay += offset; } else { currentDay -= offset; } var remainderDay = currentDay - (7 - firstWeek); if (remainderDay < 1) { result = 1; } else { result = remainderDay / 7; if (remainderDay % 7 != 0) result++; result++; } return result; }
5.統計一段時間內有多少個星期幾:
/// <summary> /// 統計一段時間內有多少個星期幾 /// </summary> /// <param name="beginDate">開始日期</param> /// <param name="endDate">結束日期</param> /// <param name="weekNumber">星期幾</param> /// <returns>多少個星期幾</returns> public static int WeekOfTotalWeeks(DateTime beginDate, DateTime endDate, DayOfWeek weekNumber) { var dayTotal = new TimeSpan(endDate.Ticks - beginDate.Ticks); var result = (int)dayTotal.TotalDays / 7; var iLen = dayTotal.TotalDays % 7; for (var i = 0; i <= iLen; i++) { if (beginDate.AddDays(i).DayOfWeek != weekNumber) continue; result++; break; } return result; }
6.計算當前月屬于第幾季度:
/// <summary> /// 計算當前月屬于第幾季度 /// </summary> /// <returns>當前年第幾季度</returns> public static int QuarterOfCurrent() { return QuarterOfCurrent(DateTime.Now.Month); } /// <summary> /// 計算某個月屬于第幾季度 /// </summary> /// <param name="month">需要計算的月份</param> /// <returns>某年第幾季度</returns> public static int QuarterOfCurrent(int month) { if (month < 1 || month > 12) return -1; return (month - 1) / 3 + 1; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。