andrewsimon
andrewsimon

Reputation: 371

How to find the first day of next month,if the current month is december

I'm using the following query to get the next month.

int theMonth = ((System.DateTime)periodStartDate).Month+1;

But if the periodstartDate month id=s december,the above statement throws error.

Upvotes: 37

Views: 48378

Answers (13)

Ebram Shehata
Ebram Shehata

Reputation: 621

Simple one:

var timeNow = DateTime.Now;
var firstDayNextMonth = timeNow.AddDays(1 - timeNow.Day).AddMonths(1);

Upvotes: 0

Johann Contuzi
Johann Contuzi

Reputation: 51

First date of next month, time 00:00:00

DateTime dt = DateTime.Now.AddMonths(1);
DateTime dayone = new DateTime(dt.Year, dt.Month, 1);

Upvotes: 2

khaled saleh
khaled saleh

Reputation: 518

You can use this code:

var newDaye = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day + 1); //first day in next month

Upvotes: 1

Kiran290
Kiran290

Reputation: 1

    DateTime nextMonthStartDate= DateTime.Parse(startdate.Year + "-" + startdate.AddMonths(1).Month + "-01");

This will work in leap year or non leap year. In the above converting strings to date time with static date 1 coz always taking start of the month. Hope it is useful

Upvotes: -2

Rubens Farias
Rubens Farias

Reputation: 57946

The trick part is to understand the start date could not start in the first day of the current month, so a plain AddMonth could lead to undesired dates. Build a new DateTime in the day 01 and, then, add the month.

var firstDayNextMonth = new DateTime(startDate.Year, startDate.Month, 1).AddMonths(+1);

BTW, the AddMonths method documentation states:

The AddMonths method calculates the resulting month and year, taking into account leap years and the number of days in a month, then adjusts the day part of the resulting DateTime object. If the resulting day is not a valid day in the resulting month, the last valid day of the resulting month is used. For example, March 31st + 1 month = April 30th, and March 31st - 1 month = February 28 for a non-leap year and February 29 for a leap year.

Upvotes: 7

Radostin Dimitrov
Radostin Dimitrov

Reputation: 1

DateTime date = DateTime.Now;
Console.WriteLine(date);
// Sunday 28.06.2015 г. 10:22:41 ч.

int monthsBack = -1;
int whichDay = 1;
// It means -> what day the first day of the previous month is.
DayOfWeek FirstDayOfWeek = date.AddMonths(monthsBack).AddDays(whichDay).DayOfWeek;
Console.WriteLine(FirstDayOfWeek);
// Friday

int delta = DayOfWeek.Monday - date.AddMonths(monthsBack).AddDays(whichDay).DayOfWeek;
Console.WriteLine(delta);
// -4
//-4 ->Monday , -3 ->Tuesday, -2 ->Wednesday , -1 ->Thursday, 0 ->Friday

Upvotes: 0

sympatric greg
sympatric greg

Reputation: 3179

I like V4V's answer, but I write it this way:

DateTime dt = new DateTime(2011,12,2);
DateTime firstDayNextMonth = dt.AddMonths(1).AddDays(-dt.Day+1);

For example, I might be computing a future time and this code does that without stripping out the time part.

Per hvd's most astute comment, this code should be:

DateTime dt = new DateTime(2011,12,2);
DateTime firstDayNextMonth = dt.AddDays(-dt.Day+1).AddMonths(1);

Upvotes: 13

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10967

   DateTime now = DateTime.Now;
        DateTime nextMonth;
        if(now.Day > 1)
         nextMonth = now.AddDays(-(now.Day - 1)).AddMonths(1);
        else
         nextMonth = now.AddMonths(1);

Where Now is the Date you want to start ,you could replace with TheStartPeriod

Upvotes: 0

V4Vendetta
V4Vendetta

Reputation: 38210

I think you can get it in this fashion

DateTime dt = new DateTime(2011,12,2);
DateTime dayone = new DateTime(dt.AddMonths(1).Year, dt.AddMonths(1).Month, 1);

Now you have a proper DateTime object to the first of the next month, do as you please with it

Upvotes: 60

Enigmativity
Enigmativity

Reputation: 117064

The expression ((System.DateTime)periodStartDate).Month+1 doesn't throw an error if the month is December - it just returns 13. I suspect you're doing this:

var nextMonth = new DateTime(periodStartDate.Year, periodStartDate.Month + 1, 1);

That would throw an error.

Try this instead:

var nextMonth = new DateTime(periodStartDate.Year, periodStartDate.Month, 1)
    .AddMonths(1);

Upvotes: 26

Paul Ruane
Paul Ruane

Reputation: 38590

If you call AddMonths(1) then .NET will automatically roll the date into the next year.

periodStartDate.AddMonths(1).Month;

Upvotes: 1

Andy
Andy

Reputation: 7826

int theMonth = ((System.DateTime)periodStartDate).AddMonths(1).Month;

Upvotes: 9

Zruty
Zruty

Reputation: 8667

after you compute theMonth, check whether it equals to 13 (the month after December) and replace the value with 1:

theMonth = theMonth==13 ? 1 : theMonth;

Upvotes: 1

Related Questions