Sandeep Sandeep - 5 months ago 20
iOS Question

How to show 7 days from current date in ios if today is last day of the month

I am showing seven days of month in

datepickerview
.

But my problem is how to show seven days from current date if todays current date is last day of the month.

Example : Suppose todays month is june, june has 30 days so todays date is 30 .Then my problem is how to show next 6 days from the current date,means in datepicker i want 30th june,1july,2july,3july,4july,5july,6july.

Please suggest me how to solve that problem. I also provided the code please suggest me where i am doing wrong with my code.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day = 7;

NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc] initWithTitle:@"Select Date" datePickerMode:UIDatePickerModeDate selectedDate:[NSDate date] minimumDate:[NSDate date] maximumDate:sevenDays target:self action:@selector(timeWasSelected:element:) origin:sender];

Answer

you can calculate next seven days by following line of code:

NSDate *sevenDays = [[NSDate date] dateByAddingTimeInterval:60*60*24*7];

Then you can use in DatePicker as of your below Code.

ActionSheetDatePicker *datePicker = [[ActionSheetDatePicker alloc] 
    initWithTitle:@"Select Date" datePickerMode:UIDatePickerModeDate 
    selectedDate:[NSDate date] minimumDate:[NSDate date] 
    maximumDate:sevenDays target:self 
    action:@selector(timeWasSelected:element:) origin:sender];

Hope This will help you.

Please add comment if you have any query regarding the same.

Happy Coding !!!