C#中的DateTime.DaysInMonth()方法
C#中的DateTime.DaysInMonth()方法用于返回指定月份和年份中的天数。例如,月份值1(即一月)为31。
语法
以下是语法-
public static int DaysInMonth (int year, int month);
示例
现在让我们看一个实现DateTime.DaysInMonth()方法的示例-
using System;
public class Demo {
public static void Main() {
DateTime date1 = new DateTime(2019, 08, 20, 6, 20, 40);
DateTime date2 = new DateTime(2019, 06, 20, 6, 20, 40);
Console.WriteLine("DateTime 1 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", date1);
Console.WriteLine("Days in DateTime 1 month = "+DateTime.DaysInMonth(2019, 08));
Console.WriteLine("\nDateTime 2 = {0:dd} {0:y}, {0:hh}:{0:mm}:{0:ss} ", date2);
Console.WriteLine("Days in DateTime 2 month = "+DateTime.DaysInMonth(2019, 06));
int res = date1.CompareTo(date2);
// returns >0 since date1 is later than date2
Console.WriteLine("\nReturn Value (comparison) = "+res);
}
}输出结果
这将产生以下输出-
DateTime 1 = 20 August 2019, 06:20:40 Days in DateTime 1 month = 31 DateTime 2 = 20 June 2019, 06:20:40 Days in DateTime 2 month = 30 Return Value (comparison) = 1
示例
现在让我们来看另一个实现DateTime.DaysInMonth()方法的示例-
using System;
public class Demo {
public static void Main(){
int year1 = 2019, year2 = 2016;
int FebMonth = 2;
Console.WriteLine("Days in 2019, Feb month = "+DateTime.DaysInMonth(year1, FebMonth));
Console.WriteLine("Days in 2016, Feb month = "+DateTime.DaysInMonth(year2, FebMonth));
}
}输出结果
这将产生以下输出-
Days in 2019, Feb month = 28 Days in 2016, Feb month = 29
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便