Vivekh
Vivekh

Reputation: 4259

How can I get the next month End Date from the given Date

I am implementing a code to find quarter start date and End date every thing was implemented fine but if user enters a date like 2011,2,1 I would like to get the quarter end date based on this date

DateTime dtTempStartDate = new DateTime(2011, 2, 1);
var qrtrDate = DateTime.ParseExact(dtTempStartDate.ToString("MM-dd-yyyy"), "MM-dd-yyyy", System.Globalization.CultureInfo.InvariantCulture);
var dtMnthEnd = qrtrDate.AddMonths(1);` 

should I add days or add milliseconds can some one help me...

Upvotes: 1

Views: 399

Answers (2)

Satendra Kumar
Satendra Kumar

Reputation: 103

If you want to know the next month end date, you can use this formula:

=DATE((YEAR(A1)),(MONTH(A1)+1),(DAY(A1)))

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292365

int quarter = (int)Math.Ceiling(qrtrDate.Month / 3.0);
int lastMonthInQuarter = 3 * quarter;
DateTime lastDayOfQuarter = new DateTime(qrtrDate.Year, lastMonthInQuarter, DateTime.DaysInMonth(qrtrDate.Year, lastMonthInQuarter));

Upvotes: 4

Related Questions