J.Doe J.Doe - 2 days ago 4
C# Question

C# - program to display all of the same days dates in the month, user input

i need guidance on how i can display for example all monday dates in a month, based on user input, can be any day and any month.
my code so far is here but doesn't seem to display the dates. of the specified day i input, where has this gone wrong, any help is really appreciated.

using System;
using System.Globalization;

namespace Calendar
{
class Program
{
static int promptDay = new int();
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());
Console.Write("Enter the day you want to see the dates for (Mon = 0, etc): ");
promptDay = Convert.ToInt32(Console.ReadLine());
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 currentDay = 1;
for (int i = 0; i < calendar.GetLength(0); i++)
{
for (int j = 0; j < calendar.GetLength(1) && currentDay <= totalDays; j++)
{
if (i == 0 && day > j)
{
calendar[i, j] = 0;
}
else
{
if (j != promptDay)
{
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("");
}
}
}
}

Sid Sid
Answer

The following code prints out all the days for a given DayOfWeek, yes it can be improved but it's just a starting point

OutPut

What is the year? : 2016
What is the name of the month? : dicembre
Monday
5 12 19 26
Thursday
1 8 15 22 29
Wednesday
7 14 21 28
Tuesday
6 13 20 27
Friday
2 9 16 23 30
Saturday
3 10 17 24 31
Sunday
4 11 18 25

Note that dicembre is december in Italian.

Dictionary<DayOfWeek, List<int>> daysCount = new Dictionary<DayOfWeek, List<int>>()
{
    { DayOfWeek.Monday, new List<int>() },
    { DayOfWeek.Thursday, new List<int>() },
    { DayOfWeek.Wednesday, new List<int>() },
    { DayOfWeek.Tuesday, new List<int>() },
    { DayOfWeek.Friday, new List<int>() },
    { DayOfWeek.Saturday, new List<int>() },
    { DayOfWeek.Sunday, new List<int>() },
};

Console.Write("What is the year? : ");
var year = Convert.ToInt32(Console.ReadLine());
Console.Write("What is the name of the month? : ");
var monthName = Console.ReadLine();

var monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
var dayOfMonth = DateTime.DaysInMonth(year, monthNumber);

var date = new DateTime(year, monthNumber, 1);


for(int i = 0; i < dayOfMonth; i++, date = date.AddDays(1))
{

    if (daysCount[date.DayOfWeek] == null)
    {
        daysCount[date.DayOfWeek] = new List<int>();
    }

    daysCount[date.DayOfWeek].Add(date.Day);
}

foreach (var day in daysCount)
{
    Console.WriteLine(day.Key.ToString());
    foreach (var dayNumber in day.Value)
    {
        Console.Write(string.Format("{0} ", dayNumber));
    }
    Console.WriteLine();

}

Console.ReadLine();

Variant to show only user specified day

Notice that Sunday = 0

var days = new List<int>();
Console.Write("What is the year? : ");
var year = Convert.ToInt32(Console.ReadLine());
Console.Write("What is the name of the month? : ");
var monthName = Console.ReadLine();
Console.Write("Enter the day you want to see the dates for (Mon = 0, etc): ");
var promptDay = Convert.ToInt32(Console.ReadLine());

var monthNumber = DateTime.ParseExact(monthName, "MMMM", CultureInfo.CurrentCulture).Month;
var dayOfMonth = DateTime.DaysInMonth(year, monthNumber);

var date = new DateTime(year, monthNumber, 1);


for(int i = 0; i < dayOfMonth; i++, date = date.AddDays(1))
{

    if ((int)date.DayOfWeek == promptDay)
        days.Add(date.Day);
}

foreach (var day in days)
{
    Console.Write(string.Format("{0} ", day));

}

Console.ReadLine();
Comments