Getting First Day of Quarter in Dot Net.

Vishalneeraj-24503
Posted by Vishalneeraj-24503 under Visual Studio category on | Points: 40 | Views : 1319
Write below code:-
public DateTime GetFirstDayofQuarter(DateTime dt)
{
int current_quarter = (dt.Month - 1) / 3 + 1;

DateTime dtFirstDay = new DateTime(dt.Year, 3 * current_quarter - 2, 1);

return dtFirstDay;
}

To test above function:-
Response.Write("First Day of Quarter is for " + DateTime.Now.ToShortDateString() + " : " + GetFirstDayofQuarter

(DateTime.Now) + "<br/>"); //8-april-2015
Response.Write("First Day of Quarter is for " + DateTime.Now.AddMonths(5).ToShortDateString() + " : " +

GetFirstDayofQuarter(DateTime.Now.AddMonths(5)) + "<br/>"); //8-september-2015

Output:-
Response.Write("First Day of Quarter is for " + DateTime.Now.ToShortDateString() + " : " + GetFirstDayofQuarter

(DateTime.Now) + "<br/>");
Response.Write("First Day of Quarter is for " + DateTime.Now.AddMonths(5).ToShortDateString() + " : " +

GetFirstDayofQuarter(DateTime.Now.AddMonths(5)) + "<br/>");

Comments or Responses

Login to post response