Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I've mentioned in the comments, there are various things you could mean by "next Tuesday", but this code gives you "the next Tuesday to occur, or today if it's already Tuesday":</p> <pre><code>DateTime today = DateTime.Today; // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysUntilTuesday = ((int) DayOfWeek.Tuesday - (int) today.DayOfWeek + 7) % 7; DateTime nextTuesday = today.AddDays(daysUntilTuesday); </code></pre> <p>If you want to give "a week's time" if it's already Tuesday, you can use:</p> <pre><code>// This finds the next Monday (or today if it's Monday) and then adds a day... so the // result is in the range [1-7] int daysUntilTuesday = (((int) DayOfWeek.Monday - (int) today.DayOfWeek + 7) % 7) + 1; </code></pre> <p>... or you could use the original formula, but from tomorrow:</p> <pre><code>DateTime tomorrow = DateTime.Today.AddDays(1); // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysUntilTuesday = ((int) DayOfWeek.Tuesday - (int) tomorrow.DayOfWeek + 7) % 7; DateTime nextTuesday = tomorrow.AddDays(daysUntilTuesday); </code></pre> <p>EDIT: Just to make this nice and versatile:</p> <pre><code>public static DateTime GetNextWeekday(DateTime start, DayOfWeek day) { // The (... + 7) % 7 ensures we end up with a value in the range [0, 6] int daysToAdd = ((int) day - (int) start.DayOfWeek + 7) % 7; return start.AddDays(daysToAdd); } </code></pre> <p>So to get the value for "today or in the next 6 days":</p> <pre><code>DateTime nextTuesday = GetNextWeekday(DateTime.Today, DayOfWeek.Tuesday); </code></pre> <p>To get the value for "the next Tuesday excluding today":</p> <pre><code>DateTime nextTuesday = GetNextWeekday(DateTime.Today.AddDays(1), DayOfWeek.Tuesday); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload