I'm working on a console based calendar as an educational task (school). It needs to display a calendar sheet for the specific month the user enters. I'm almost there, but I noticed that now all months start on a monday. I created the sheet with a stringbuilder, and I tried to fill the days before the first day of the month actually comes with zeroes, but it doesn't work yet. The for-loop is my attempt. Anyone got any ideas? I'm still learning. Here is the code that builds the sheet, whichs works fine aside from that issue:
DateTime date = DateTime.Now;
List<DateTime> dateList = new List<DateTime>();
DateTime dateCopyForModification = date;
int inputMonth = date.Month;
while (dateCopyForModification.Month == inputMonth)
{
dateList.Add(dateCopyForModification);
dateCopyForModification = dateCopyForModification.AddDays(1);
}
Console.WriteLine("\t---------\n\t{0}, {1}\n\t---------", date.ToString("MMMM"), date.Year);
int dayCounter = 0;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("\nMo\t|\tDi\t|\tMi\t|\tDo\t|\tFr\t|\tSa\t|\tSo\t|\n-----------------------------------------------------------------------------------------------------\n");
foreach (DateTime dateTime in dateList)
{
//the part that doesnt work
int day = (int)dateTime.DayOfWeek;
if (dateTime == new DateTime(dateTime.Year, dateTime.Month, 1))
{
for (day = 1; day == (int)dateTime.DayOfWeek; day++)
{
stringBuilder.Append("0");
dayCounter++;
}
}
//until here
else
{
stringBuilder.Append(dateTime.Day);
dayCounter++;
}
if (dateTime.Day == 28)
{
stringBuilder.Append(" |");
}
else
{
stringBuilder.Append("\t|\t");
}
if (dayCounter == 7)
{
stringBuilder.Append("\n");
dayCounter = 0;
}
}
Console.Write(stringBuilder);
Console.ReadKey();