Posted By

stirfryrose on 07/25/12


Tagged


Versions (?)

Get Next Date


 / Published in: C#
 

  1. // <summary>
  2. /// Finds the next date whose day of the week equals the specified day of the week.
  3. /// </summary>
  4. /// <param name="startDate">
  5. /// The date to begin the search.
  6. /// </param>
  7. /// <param name="desiredDay">
  8. /// The desired day of the week whose date will be returneed.
  9. /// </param>
  10. /// <returns>
  11. /// The returned date is on the given day of this week.
  12. /// If the given day is before given date, the date for the
  13. /// following week's desired day is returned.
  14. /// </returns>
  15. public static DateTime GetNextDateForDay( DateTime startDate, DayOfWeek desiredDay )
  16. {
  17. // (There has to be a better way to do this, perhaps mathematically.)
  18. // Traverse this week
  19. DateTime nextDate = startDate;
  20. while( nextDate.DayOfWeek != desiredDay )
  21. nextDate = nextDate.AddDays( 1D );
  22.  
  23. return nextDate;
  24. }

Report this snippet  

You need to login to post a comment.