Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting A particular Day in a date

Tags:

c#

ajax

asp.net

I am using Calender Extender Control in the AjaxControlToolkit. There are basically 2 controls of date : Start Date and End date (both associated with calender extender). Based on start Date selected, I populate date in the end date field like adding no of months or days. But like I have been able to add months, but also wants to set a particular day of that month which I am unable to do.

Example: Today date is 18 Dec 2012. Something like 1st of every three months, So I add 3 months the month comes out to be Feb 2013. But I want to set Day 1st Feb 2013. I am unable to do it. Kindly help.

like image 390
Rahul2788 Avatar asked Dec 18 '12 06:12

Rahul2788


Video Answer


2 Answers

You can set whatever day of month by add month.

DateTime todayDate = DateTime.Now;
DateTime after3MonthDate = todayDate.AddMonths(3);
//Set First Day of Month
after3MonthDate = new DateTime(after3MonthDate.Year, after3MonthDate.Month, 1);
like image 156
Naresh Pansuriya Avatar answered Oct 17 '22 09:10

Naresh Pansuriya


This code can be used for existing date time variable to set the day part to the first day of the month:

if(myDate.Day > 1)
{
    myDate = myDate.AddDays(-(myDate.Day - 1));
}
like image 30
st_stefanov Avatar answered Oct 17 '22 07:10

st_stefanov