How to get First, Last and No Of days in month using C#

No.of Views2201
Bookmarked0 times
By  RRaveen   On  28 Sep 2010 21:09:01
Tag : CSharp , DateTime Controls
This snippet has methods to get the first/last day of the month and No of days in month.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



In my project, I required to cache data based on the no of days by start and end of the month, so I have to take the first / last days in month using following snippet. here those for you as well.


using System;using System.Collections.Generic;using System.Text;namespace WindowsApplication1
{public class DateTimeUtility{public static DateTime GetFirstDayOfMonth(DateTime dtDate){DateTime dtFrom = dtDate;dtFrom = dtFrom.AddDays(-(dtFrom.Day - 1));return dtFrom;}public static DateTime GetFirstDayOfMonth(int Month){DateTime dtFrom = new DateTime(DateTime.Now.Year, Month, 1);return GetFirstDayOfMonth(dtFrom);}public static int GetNoOfDaysinMonth(int year, int month){return DateTime.DaysInMonth(year, month);}public static int GetNoOfDaysinMonth(DateTime dtDate){return GetNoOfDaysinMonth(dtDate.Year, dtDate.Month);}public static DateTime GetLastDayOfMonth(DateTime dtDate){DateTime dtTo = dtDate;dtTo = dtTo.AddMonths(1);dtTo = dtTo.AddDays(-(dtTo.Day));return dtTo;}public static DateTime GetLastDayOfMonth(int month){DateTime dtTo = new DateTime(DateTime.Now.Year, month, 1);return GetLastDayOfMonth(dtTo);}}}

 Hope help this you as well.

Sign Up to vote for this article
About Author
Occupation-Software Engineer
Member Type-Gold
Joined date-03 Jun 2009
Blog Page
- B.Sc. degree in Computer Science. - 4+ years experience in Visual and - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
Other popularSectionarticles
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top