61

I have here 2 datepicker for start date and end date.

how can I get the first day and last day of the current month

rdpStartDate.SelectedDate = DateTime.Now;
rdpEndDate.SelectedDate = DateTime.Now;
user1647667
  • 1,269
  • 4
  • 13
  • 26

6 Answers6

151
DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
Chris Shao
  • 8,141
  • 3
  • 38
  • 37
  • If you want to get to the final time for the month, then use AddTicks(-1) instead of AddDays(-1) – InquisitorJax Mar 07 '19 at 19:47
  • 1
    @InquisitorJax In that case, I would favor just using `` in your logic instead of `<=` or `>=` if at all possible. You can run into precision bugs with other systems if, for example, your program thinks the cutoff is `<= 2019-10-31T23:59:59.9999999`, but the database server or remote server interprets the cutoff as `<= 2019-10-31T23:59:59.999`. You won't have that problem if you specify `< 2019-11-01T00:00:00.0000000` instead. – Bacon Bits Oct 16 '19 at 14:09
38
var now = DateTime.Now;
var first = new DateTime(now.Year, now.Month, 1);
var last = first.AddMonths(1).AddDays(-1);

You could also use DateTime.DaysInMonth method:

var last = new DateTime(now.Year, now.Month, DateTime.DaysInMonth(now.Year, now.Month));
MarcinJuraszek
  • 121,297
  • 15
  • 183
  • 252
12
var myDate = DateTime.Now;
var startOfMonth = new DateTime(myDate.Year, myDate.Month, 1);
var endOfMonth = startOfMonth.AddMonths(1).AddDays(-1);

That should give you what you need.

TyCobb
  • 8,691
  • 1
  • 30
  • 52
7

Try this code it is already built in c#

int lastDay = DateTime.DaysInMonth (2014, 2);

and the first day is always 1.

Good Luck!

Jade
  • 2,922
  • 1
  • 10
  • 9
2

An alternative way is to use DateTime.DaysInMonth to get the number of days in the current month as suggested by @Jade

Since we know the first day of the month will always 1 we can use it as default for the first day with the current Month & year as current.year,current.Month,1.

var now = DateTime.Now; // get the current DateTime 

//Get the number of days in the current month
int daysInMonth = DateTime.DaysInMonth (now.Year, now.Month); 
    
//First day of the month is always 1
var firstDay = new DateTime(now.Year,now.Month,1); 
    
//Last day will be similar to the number of days calculated above
var lastDay = new DateTime(now.Year,now.Month,daysInMonth);

//So
rdpStartDate.SelectedDate = firstDay;
rdpEndDate.SelectedDate = lastDay; 
Vinod Srivastav
  • 3,066
  • 1
  • 23
  • 34
0
string firstdayofyear = new DateTime(DateTime.Now.Year, 1, 1).ToString("MM-dd-yyyy");
string lastdayofyear = new DateTime(DateTime.Now.Year, 12, 31).ToString("MM-dd-yyyy");
string firstdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("MM-dd-yyyy");
string lastdayofmonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1).ToString("MM-dd-yyyy");
moondaisy
  • 3,995
  • 6
  • 34
  • 65
  • 2
    Please add some explanatory text to your answer. Why strings? How does this answer improve on the many answers already posted? – Stephen Kennedy Apr 05 '18 at 10:36
  • This doesn't attempt to answer the question in any different way rather uses the same logic as answered above ad adds unnecessary **firstdayofyear** and **lastdayofyear** which is not even asked here. – Vinod Srivastav Jul 20 '20 at 09:15