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
  • 14
  • 26

7 Answers7

157
DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);
Chris Shao
  • 8,231
  • 3
  • 39
  • 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
  • 2
    @InquisitorJax In that case, I would favor just using `<` or `>` 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
  • @BaconBits what do you mean by < or > – Ryan Speciale Oct 05 '22 at 22:30
39
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
  • 124,003
  • 15
  • 196
  • 263
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,909
  • 1
  • 33
  • 53
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,972
  • 1
  • 11
  • 9
4

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,644
  • 1
  • 27
  • 40
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
  • 4,303
  • 6
  • 41
  • 70
  • 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
0

If, for example, the day part of a DateTime is 21 then subtract 20 days from it to get the first day of corresponding month:

DateTime today = DateTime.Now.Date;
DateTime firstDay = today.AddDays(-today.Day + 1);
DateTime lastDay = today.AddDays(-today.Day + 1).AddMonths(1).AddDays(-1);

Sample output:

today    = 03/21/2023 12:00:00 AM
firstDay = 03/01/2023 12:00:00 AM
lastDay  = 03/31/2023 12:00:00 AM
Salman A
  • 262,204
  • 82
  • 430
  • 521