C# 获取下周日期

JoeYD / 2023-08-24 / 原文

获取下周一 至 下周日

 

 DateTime today = DateTime.Today;
            int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7;
            //if today is monday, add seven days
            if (daysUntilMonday == 0)
                daysUntilMonday = 7;

            //create DateTime variables for next week's beginning and end
            DateTime nextWeekMonday = today.AddDays(daysUntilMonday);
            DateTime nextWeekSunday = nextWeekMonday.AddDays(6);

            string JHRQ = nextWeekMonday.ToString("yyyy-MM-dd") + "" + nextWeekSunday.ToString("yyyy-MM-dd");