C# - 编写一个显示日历的程序,仅询问年份和月份,使用System.DateTime

时间:2016-12-01 13:44:27

标签: c# datetime system

我坚持这个,我需要帮助我如何要求输入任何年份和任何月份的用户输入,然后将输出指定年份和月份的月度日历,我还需要使用system.datetime 这是我到目前为止的代码,我不认为这是正确的,任何帮助表示赞赏。感谢

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("");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这应该确保月中的某一天与星期几正确对齐(即12月1日是星期二)

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("");
        }
    }
}

}

修改

如果您希望获得所选月份中某一天特定日期的所有日期,您可以执行以下操作;

        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));
            }
        }

您必须要求用户输入一周中的某一天并将weekDay设置为他们输入的值。请记住星期日是0,星期六= 6.(我还没有完全测试过,所以要小心) - 这必须输入你的FillCalendar方法。