Tuesday, July 6, 2010

C#.Net : Method to get Last date of Month & Julian Date


We had a requirment to run an application for the last day of the month. The following method was helpful to get the date.

Sample Code:

private DateTime GetLastDayofMonth(DateTime dtDate)


DateTime dtToDate = dtDate;

dtToDate = dtToDate.AddMonths(1);

dtToDate = dtToDate.AddDays(-(dtToDate.Day));

return dtToDate;


// Later this code was helpful to convert it to julian date:

public string GetJulian_LastDayofPreviousMonth()


int intJulianDate = 1000 * (DateTime.Now.AddMonths(-1).Year - 1900) + GetLastDayOfMonth(DateTime.Now.AddMonths(-1)).DayOfYear;

return intJulianDate.ToString();


Hope it Helps

Fauzi ~4z