开发者

How to add one month in current month?

开发者 https://www.devze.com 2023-01-19 12:16 出处:网络
Suppose the current month is \"Oct\". I want that it will add one month in current month i.e it will show \"Nov\". For this my code is written below but it gives exception that Input string was not in

Suppose the current month is "Oct". I want that it will add one month in current month i.e it will show "Nov". For this my code is written below but it gives exception that Input string was not in a correct format.

So please correct the code?

if (Convert.ToInt32(ddlMonth.SelectedIndex ) <= Convert.ToInt32(DateTime.Now.AddMonths(1).ToString()))
            {
                TotalBalanceUptoSelectedPreviousMonth();
          开发者_JAVA技巧  }


This should probably work:

if (Convert.ToInt32(ddlMonth.SelectedIndex ) 
    <= Convert.ToInt32(DateTime.Now.AddMonths(1).ToString("M")))

    TotalBalanceUptoSelectedPreviousMonth();

However, it looks simpler like this:

if (Convert.ToInt32(ddlMonth.SelectedIndex) <= DateTime.Now.AddMonths(1).Month)
    Total...();


Convert.ToInt32(DateTime.Now.AddMonths(1).ToString())

probably gives the exception since DateTime.Now.AddMonths(1).ToString() value is not castable to Int32.


I'll go out on a limb and say that this is what you're looking for:

if(ddlMonth.SelectedIndex <= DateTime.Now.AddMonths(1).Month)
{
    TotalBalanceUptoSelectedPreviousMonth();
}

Instead of getting the date as a string and converting it, why not use the Month property of the DateTime struct (which is already an integer)?

(Oh...and SelectedIndex is already an integer as well, no need for the call to Convert)


You need to use DateTime.Now.AddMonths(1).Month.
Also you do not need to use the ToString method wrapped in a Convert.ToInt32 method as this is already an integer.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号