Monday, March 09, 2009

StartDate and Endate of the Month in C# and SQL Server

StartDate and Endate of the Month in C#

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



StartDate and Endate of the Month in SQL Server
select convert(nvarchar(8),dateadd(mm, datediff(mm, 0, '3/4/2009'), 0), 112)
select convert(nvarchar(8),dateadd( dd, -1, dateadd( mm, 1, dateadd( dd, -day('2/2/2009')+1, '2/2/2009'))),112)

Example in C#:: To get First day of the Month and Last day of the Month Using C#

DateTime givenDate=DateTime.Parse(TxtAppDate.Text);

int year=givenDate.Year;
int month=givenDate.Month;

DateTime startDate= new DateTime(year, month, 1);
DateTime endDate = startDate.AddMonths(1).AddDays(-1);

while(startDate <= endDate)
{
string appdate=startDate.ToShortDateString();
GetDetails(appdate);
startDate=startDate.AddDays(1);
}