Adding one day to the last day of the month

DateTime dt = DateTime.UtcNow;  

var lastDayOfMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
day = dt.Day;
if (day != lastDayOfMonth)
{
    if (dt.Hour >= 14)
    {
        day = day + 1;
    }
    else if (dt.DayOfWeek == DayOfWeek.Sunday)
        day = day + 1;
}
else
{
    //  
}

Now I want to add one day on the last day of the month. How can i do this?

+3
source share
5 answers

Finally, I did this by removing the if condition for lastDayOfMonth and using the AddDays property.

                if (dt.Hour >= 14)
                {
                    dt.AddDays(1);
                }
                else if (dt.DayOfWeek == DayOfWeek.Sunday)
                {
                    dt.AddDays(1);
                }
                day = dt.Day;

Thank you all for your valuable suggestions.

+1
source

Try

newDate = date.AddDays(1);
+3
source

, DateTime.AddDays

DateTime, .

DateTime.Day int. 1, .

day = day + 1;

dt, .

, .

1, 1 - .

:

DateTime dt = DateTime.UtcNow;
var FirstDayOfNextMonth = new DateTime(dt.Year, dt.Month, 1).AddMonths(1);
var LastDayOfCurrentMonth = FirstDayOfNextMonth.AddDays(-1);
Console.WriteLine(LastDayOfCurrentMonth); 

:

2/28/2014 12:00:00 AM

demonstration.

+2
DateTime today = DateTime.Today;
DateTime firstDayOfThisMonth = new DateTime(today.Year, today.Month, 1);
DateTime firstDayOfNextMonth = firstDayOfThisMonth.AddMonths(1);
+1

To find the first day of the next month, you can use

Datetime FirstDayofNxtMnth =DATE(YEAR(TODAY()),MONTH(TODAY())+1,1);

To find the last day of this month, you can use

Datetime FirstDayofNxtMnth =DATE(YEAR(TODAY()),MONTH(TODAY())+1,1);

Datetime LastDayofThisMnth =DATE(YEAR(TODAY()),MONTH(TODAY())+1,1).addDay(-1);

+1
source

All Articles