發布時間:2020-08-06 15:54 作者:獨孤劍 閱讀:3168
using System; namespace ConsoleApp2 { class Program { static void Main(string[] args) { Console.WriteLine("本周第一天:{0}", GetWeekFirstDate(DateTime.Now)); Console.Read(); } /// <summary> /// 獲取本周第一天(以星期一為第一天) /// </summary> /// <param name="dateTime">當前時間 DateTime.Now or DateTime.UtcNow</param> /// <returns>后面的具體時、分、秒和傳入值相等</returns> public static DateTime GetWeekFirstDate(DateTime dateTime) { int weeknow = Convert.ToInt32(dateTime.DayOfWeek); // 因為是以星期一為第一天,所以要判斷weeknow等于0時,要向前推6天。 weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1)); int daydiff = (-1) * weeknow; // 本周第一天 return Convert.ToDateTime(dateTime.AddDays(daydiff)); } } }
微信打賞, 微信掃一掃
支付寶打賞, 支付寶掃一掃
如果文章對您有幫助,歡迎給作者打賞