How do I generate date for each day in the next week

Mohammed Faraj 1 Reputation point
2022-08-18T05:29:27.707+00:00

using System;

namespace Learning
{
class Program
{
static void Main(string[] args)
{

        PhysicalActivity myLesson = new PhysicalActivity();  
        myLesson.Day = "Monday";  
        myLesson.Lesson = "Teusday";  
        myLesson.Activity = "Wednesday";  
        myLesson.Leder = "Thursday";  
        myLesson.Time = "Friday";  


        /*myLesson.Day = "Monday";  
        myLesson.Day = "Teusday";  
        myLesson.Day = "Wednsday";  
        myLesson.Day = "Thursday";  
        myLesson.Day = "Friday";*/  



    }  
}  

}

Developer technologies | .NET | .NET CLI
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-08-19T02:09:36.587+00:00

    Add the following class to your project.

    public static class DateTimeHelpers  
    {  
        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);  
        }  
      
        public static List<DateTime> NextWeeksDates() =>  
            Enumerable.Range(0, 7).Select(index =>   
                DateTime.Now.Next(DayOfWeek.Sunday).AddDays(index)).ToList();  
    }  
    

    Get dates

    foreach (var dateTime in DateTimeHelpers.NextWeeksDates())  
    {  
        Console.WriteLine(dateTime.ToShortDateString());  
    }  
    
    1 person found this answer helpful.
    0 comments No comments

  2. CowieCowie 76 Reputation points
    2022-08-18T07:44:15.883+00:00

    Hi MohammedFaraj, guess this is what you ask for.

    Today is 8/19/2022 and with the code below, you can generate the date for each day in the next week:

                string NextSun = DateTime.Now.AddDays(0 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
                string NextMon = DateTime.Now.AddDays(1 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
                string NextTue = DateTime.Now.AddDays(2 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
                string NextWed = DateTime.Now.AddDays(3 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
                string NextThu = DateTime.Now.AddDays(4 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
                string NextFri = DateTime.Now.AddDays(5 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
                string NextSat = DateTime.Now.AddDays(6 - Convert.ToInt16(DateTime.Now.DayOfWeek) + 7).ToString();  
      
                Console.WriteLine(NextSun);  
                Console.WriteLine(NextMon);  
                Console.WriteLine(NextTue);  
                Console.WriteLine(NextWed);  
                Console.WriteLine(NextThu);  
                Console.WriteLine(NextFri);  
                Console.WriteLine(NextSat);  
    

    Just copy the code to your project and you can see the result below:
    232625-2.png

    If you still feel confused, please comment and we can discuss more about it. If you are satisfied with my answer, please accept my answer and kindly upvote it!
    Happy coding!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.