![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DateTime.DaysInMonth() Method in C#
The DateTime.DaysInMonth() method in C# is used to return the number of days in the specified month and year. For example, 31 for month value 1 i.e. January.
Syntax
Following is the syntax −
public static int DaysInMonth (int year, int month);
Example
Let us now see an example to implement the DateTime.DaysInMonth() method −
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("
DateTime 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("
Return Value (comparison) = "+res); } }
Output
This will produce the following output −
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
Example
Let us now see another example to implement the DateTime.DaysInMonth() method −
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)); } }
Output
This will produce the following output −
Days in 2019, Feb month = 28 Days in 2016, Feb month = 29
Advertisements