# 如何得到下个星期天的date？

ASP.net得到下一个星期二

### 8 Solutions collect form web for “如何得到下个星期天的date？”

` ` public static DateTime Next(this DateTime from, DayOfWeek dayOfWeek) { int start = (int)from.DayOfWeek; int target = (int)dayOfWeek; if (target <= start) target += 7; return from.AddDays(target - start); }` `

`date.AddDays(7 - (int)date.DayOfWeek)`应该这样做，我想。

`date.DayOfWeek`将返回一个表示date的枚举值（其中0表示星期日）。

` ` var date = DateTime.Now; var nextSunday = date.AddDays(7 - (int) date.DayOfWeek);` `

` `var nearestSunday = date.AddDays(7 - date.DayOfWeek == DayOfWeek.Sunday ? 7 : date.DayOfWeek);` `
` `/// <summary> /// Finds the next date whose day of the week equals the specified day of the week. /// </summary> /// <param name="startDate"> /// The date to begin the search. /// </param> /// <param name="desiredDay"> /// The desired day of the week whose date will be returneed. /// </param> /// <returns> /// The returned date is on the given day of this week. /// If the given day is before given date, the date for the /// following week's desired day is returned. /// </returns> public static DateTime GetNextDateForDay( DateTime startDate, DayOfWeek desiredDay ) { // (There has to be a better way to do this, perhaps mathematically.) // Traverse this week DateTime nextDate = startDate; while( nextDate.DayOfWeek != desiredDay ) nextDate = nextDate.AddDays( 1D ); return nextDate; }` `

` `int dayOfWeek = (int) DateTime.Now.DayOfWeek; DateTime nextSunday = DateTime.Now.AddDays(7 - dayOfWeek).Date;` `

` `DateTime dt=dateTime; do { dt=dt.AddDays(1); } while(dt.DayOfWeek!= DayOfWeek.Sunday); // 'dt' is now the next Sunday` `

` ` private static DateTime GetNextSunday(DateTime dt) { var tomorrow = dt.AddDays(1); if (tomorrow.DayOfWeek != DayOfWeek.Sunday) { GetNextSunday(tomorrow); } return tomorrow; }` `
` `public static DateTime GetNextSunday (DateTime date) { if (date.DayOfWeek == DayOfWeek.Sunday) date = date.AddDays(1); while (date.DayOfWeek != DayOfWeek.Sunday) date = date.AddDays(1); return date; }` `
• 我如何循环浏览date范围？
• 在C＃中创buildMidnight DateTime的最佳方法
• 我如何parsing和转换DateTime的RFC 3339date时间格式？
• 在HQL中执行date/时间math？
• 从date范围中生成date
• 使用strftime将python datetime转换为纪元
• 在C＃中将stringparsing为DateTime
• 你怎么find这个月的最后一天？
• 在C＃中添加时间到date时间
• 如何比较date在C＃