在C#中,可以使用DateTime类的DaysInMonth方法来获取一个月当中有多少天。该方法接受两个参数,即年份和月份,返回指定年份和月份的天数。以下是一个示例代码:

using System;

class Program
{
    static void Main(string[] args)
    {
        int year = 2022;
        int month = 2;

        int daysInMonth = DateTime.DaysInMonth(year, month);

        Console.WriteLine("There are {0} days in {1}/{2}.", daysInMonth, month, year);
    }
}

在上述代码中,我们指定了年份为2022年,月份为2月。然后使用DateTime.DaysInMonth方法获取这个月的天数,并将结果输出到控制台。输出结果为:"There are 28 days in 2/2022.",表示2022年2月有28天。

你可以根据需要修改年份和月份来获取不同月份的天数。

C# 获取一个月当中有多少天

原文地址: http://www.cveoy.top/t/topic/jdaL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录