Search code examples
c#datetimesystem

C# - write a program that displays the calendar, asking just for the year and month, use System.DateTime


I am stuck on this, i need help on how i can ask for user input to enter any year and any month, then that will output a monthly calendar for that specified year and month, i also need to use system.datetime This is the code i have so far and i dont think it is correct, any help is appreciated. Thanks

class Program
{
    static int year = new int();
    static int month = new int();
    static int[,] calendar = new int[6, 7];

    static void Main(string[] args)
    {
        Console.Write("Enter the year? ");
        year = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter the month (January = 1, etc): ");
        month = Convert.ToInt32(Console.ReadLine());

        DrawHeader();
        FillCalendar();
        DrawCalendar();
        Console.ReadLine();
    }

    static void DrawHeader()
    {
        Console.Write("\n\n");
        Console.WriteLine("\t" + month);
        Console.WriteLine("Mo Tu We Th Fr Sa Su");
    }

    static void FillCalendar()
    {
        int days = DateTime.DaysInMonth(year, month);
        int currentDay = 1;
        for (int i = 0; i < calendar.GetLength(0); i++)
        {
            for (int j = 0; j < calendar.GetLength(1) && currentDay <= days; j++)
            {
                if (i == 0 && month > j)
                {
                    calendar[i, j] = 0;
                }
                else
                {
                    calendar[i, j] = currentDay;
                    currentDay++;
                }
            }
        }
    }

    static void DrawCalendar()
    {
        for (int i = 0; i < calendar.GetLength(0); i++)
        {
            for (int j = 0; j < calendar.GetLength(1); j++)
            {
                if (calendar[i, j] > 0)
                {
                    if (calendar[i, j] < 10)
                    {
                        Console.Write(" " + calendar[i, j] + " ");
                    }
                    else
                    {
                        Console.Write(calendar[i, j] + " ");
                    }
                }
                else
                {
                    Console.Write("   ");
                }
            }
            Console.WriteLine("");
        }
    }
}

Solution

  • This should ensure that the day of the month is correctly aligned with the day of the week (i.e. December 1st is a Tuesday)

    using System;
    using System.Globalization;
    
    namespace Calendar
    {
    class Program
    {
        static int year = new int();
        static int month = new int();
        static int[,] calendar = new int[6, 7];
        private static DateTime date;
    
        static void Main(string[] args)
        {
            Console.Write("Enter the year? ");
            year = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter the month (January = 1, etc): ");
            month = Convert.ToInt32(Console.ReadLine());
    
            date = new DateTime(year, month, 1);//gives you a datetime object for the first day of the month
            DrawHeader();
            FillCalendar();
            DrawCalendar();
            Console.ReadLine();
        }
    
        static void DrawHeader()
        {
            Console.Write("\n\n");
            //gives you the month and year at the top of the calendar
            Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month) + " " + year);
            Console.WriteLine("Mo Tu We Th Fr Sa Su");
        }
    
        static void FillCalendar()
        {
            int days = DateTime.DaysInMonth(year, month);
            int currentDay = 1;
            var dayOfWeek = (int) date.DayOfWeek;
            for (int i = 0; i < calendar.GetLength(0); i++)
            {
                for (int j = 0; j < calendar.GetLength(1) && currentDay - dayOfWeek + 1 <= days; j++)
                {
                    if (i == 0 && month > j)
                    {
                        calendar[i, j] = 0;
                    }
                    else
                    {
                        calendar[i, j] = currentDay - dayOfWeek + 1;
                        currentDay++;
                    }
                }
            }
        }
    
        static void DrawCalendar()
        {
            for (int i = 0; i < calendar.GetLength(0); i++)
            {
                for (int j = 0; j < calendar.GetLength(1); j++)
                {
                    if (calendar[i, j] > 0)
                    {
                        if (calendar[i, j] < 10)
                        {
                            Console.Write(" " + calendar[i, j] + " ");
                        }
                        else
                        {
                            Console.Write(calendar[i, j] + " ");
                        }
                    }
                    else
                    {
                        Console.Write("   ");
                    }
                }
                Console.WriteLine("");
            }
        }
    }
    

    }

    EDIT

    If you want to get all the all the dates for a particular day of the week in a chosen month you can do something like this;

            var weekDay = 0;
            var dates = new List<DateTime>();
            for (int i = 0; i < days; i++)
            {
                if ((int)date.AddDays(i).DayOfWeek == weekDay)
                {
                    dates.Add(date.AddDays(i));
                }
            }
    

    You would have to ask the user to enter a day of the week and set weekDay to the value they enter. Just remember that Sunday is 0 and Saturday = 6. (I haven't fully tested this though so be careful) - This would have to be entered into your FillCalendar method.