本文整理匯總了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);
}
}
輸出:
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();
}
}
}
輸出:
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);
}
}