user1647667
user1647667

Reputation: 1309

Getting first and last day of the current month

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;

Upvotes: 65

Views: 156134

Answers (7)

Jade
Jade

Reputation: 2992

Try this code; it is already built in C#

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

and the first day is always 1.

Upvotes: 7

Salman Arshad
Salman Arshad

Reputation: 272006

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

Upvotes: 0

Vinod Srivastav
Vinod Srivastav

Reputation: 4255

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; 

Upvotes: 4

Bhagawan Shirsath
Bhagawan Shirsath

Reputation: 11

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");

Upvotes: 0

Chris Shao
Chris Shao

Reputation: 8231

DateTime now = DateTime.Now;
var startDate = new DateTime(now.Year, now.Month, 1);
var endDate = startDate.AddMonths(1).AddDays(-1);

Upvotes: 159

TyCobb
TyCobb

Reputation: 9089

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.

Upvotes: 12

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

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));

Upvotes: 42

Related Questions