Click here to Skip to main content
15,910,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there are two date time i need to check whether they are in same month of same year

ex:'2014-01-01'--'2014-01-25' both are in january 2014
Posted

You can use the System.DateTime class. http://msdn.microsoft.com/en-us/library/system.datetime.month(v=vs.110).aspx[^]

It has example code in there. You can create 2 new instances of System.DateTime of your 2 dates and then compare dateTime1.Month and dateTime2.Month as well as .Year on both.

Because they are dates, there are also many other ways you can do this but this would be a simple way.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Sep-14 10:23am    
Sure, a 5.
—SA
C#
DateTime d1 = new DateTime(2014, 9, 19, 7, 0, 0); // 2014-09-19 7:00:00
DateTime d2 = new DateTime(2014, 9, 25, 7, 0, 0); // 2014-09-25 7:00:00

if (d1.Month == d2.Month && d1.Year == d2.Year)
{
    // do what you need
}
 
Share this answer
 
Comments
ZurdoDev 25-Sep-14 15:19pm    
+5. Good example.
Member 13847218 1-Aug-18 3:18am    
how do date month year calculate
i think warranty monthily duration

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900