当前位置: 首页>>代码示例>>C#>>正文


C# DateTime.DaysInMonth方法代码示例

本文整理汇总了C#中System.DateTime.DaysInMonth方法的典型用法代码示例。如果您正苦于以下问题:C# DateTime.DaysInMonth方法的具体用法?C# DateTime.DaysInMonth怎么用?C# DateTime.DaysInMonth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.DateTime的用法示例。


在下文中一共展示了DateTime.DaysInMonth方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;

class Example
{
    static void Main()
    {
        const int July = 7;
        const int Feb = 2;

        int daysInJuly = System.DateTime.DaysInMonth(2001, July);
        Console.WriteLine(daysInJuly);
        
        // daysInFeb gets 28 because the year 1998 was not a leap year.
        int daysInFeb = System.DateTime.DaysInMonth(1998, Feb);
        Console.WriteLine(daysInFeb);
        
        // daysInFebLeap gets 29 because the year 1996 was a leap year.
        int daysInFebLeap = System.DateTime.DaysInMonth(1996, Feb);
        Console.WriteLine(daysInFebLeap);
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:22,代码来源:DateTime.DaysInMonth

输出:

31
28
29

示例2: Main

//引入命名空间
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      int[] years = { 2012, 2014 };
      DateTimeFormatInfo dtfi = DateTimeFormatInfo.CurrentInfo;
      Console.WriteLine("Days in the Month for the {0} culture " +
                        "using the {1} calendar\n", 
                        CultureInfo.CurrentCulture.Name, 
                        dtfi.Calendar.GetType().Name.Replace("Calendar", ""));
      Console.WriteLine("{0,-10}{1,-15}{2,4}\n", "Year", "Month", "Days");
      
      foreach (var year in years) {
         for (int ctr = 0; ctr <= dtfi.MonthNames.Length - 1; ctr++) {
            if (String.IsNullOrEmpty(dtfi.MonthNames[ctr]))  
               continue;
            
            Console.WriteLine("{0,-10}{1,-15}{2,4}", year, 
                              dtfi.MonthNames[ctr], 
                              DateTime.DaysInMonth(year, ctr + 1));
         }
         Console.WriteLine(); 
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:29,代码来源:DateTime.DaysInMonth

输出:

Days in the Month for the en-US culture using the Gregorian calendar

Year      Month          Days

2012      January          31
2012      February         29
2012      March            31
2012      April            30
2012      May              31
2012      June             30
2012      July             31
2012      August           31
2012      September        30
2012      October          31
2012      November         30
2012      December         31

2014      January          31
2014      February         28
2014      March            31
2014      April            30
2014      May              31
2014      June             30
2014      July             31
2014      August           31
2014      September        30
2014      October          31
2014      November         30
2014      December         31

示例3: Main

//引入命名空间
using System;

class MainClass
{
  public static void Main()
  {

    int days = DateTime.DaysInMonth(2004, 1);
    Console.WriteLine("DateTime.DaysInMonth(2004, 1) = " + days);
  }

}
开发者ID:C#程序员,项目名称:System,代码行数:13,代码来源:DateTime.DaysInMonth


注:本文中的System.DateTime.DaysInMonth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。