Search code examples
iosipadnscalendar

same day of last month ios


Is there any way using NSCalender you can get same day of last month?

I am using a calender which shows user 1 month like the iPad calender when they click on the button I want to move to previous month but should select the same day as before.

I want to just do

[components setMonth:([components month] - 1)];

but this will create problems when I are moving from a month with 31 days to month with 30 days and selected day is 31st.

I was able to find examples for android but not iOS.

android example

Any help would be appreciated


Solution

  • Check out this link.

    It's about adding one month, but you could probably do the same with subtracting.

    Change

    [dateComponents setMonth:1];
    

    into

    [dateComponents setMonth:-1];