Sandeep
Sandeep

Reputation: 723

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];

Upvotes: 3

Views: 3181

Answers (5)

Abha
Abha

Reputation: 1082

for (int i = 0; i < 7; i ++)
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =i;
    NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:objDate.date options:0];
    [dateFormatter setDateFormat:@"dd/MM/yy"];

    NSString *str = [dateFormatter stringFromDate:sevenDays];
    NSLog(@"str : %@",str);
}

Hope this code will help's you :)

Upvotes: 0

Abhishek
Abhishek

Reputation: 494

Code seems to be correct, maybe issue is with third party picker you are using? Try you code with UIDatePicker and see if you get results you are looking for, if so it means ActionSheetDatePicker has some issues....

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

NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:  [NSDate date] options:0];

Below code in with UIDatePicker looks like this...._datePicker is iboutlet UIDatePicker

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

NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
[_datePicker setMinimumDate:[NSDate date]];
[_datePicker setMaximumDate:sevenDays];

In UIDatePicker with above code, if user scrolls to previous date picker will automatically sets on todays date, and same for 7th July if user scroll to 10th picker will automatically sets to 7th july current date...

Upvotes: 2

vadian
vadian

Reputation: 285069

This creates an array nextSevenDays containing the next 7 days including today.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *startDate = [calendar startOfDayForDate:[NSDate date]];
NSMutableArray<NSDate*> * nextSevenDays = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < 7; i++) {
  [nextSevenDays addObject:[calendar dateByAddingUnit:NSCalendarUnitDay value: i toDate: startDate options: NSCalendarMatchNextTime]];
}
NSLog(@"%@", nextSevenDays);

To get the date 7 days from now use

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *startDate = [calendar startOfDayForDate:[NSDate date]];
NSDate *sevenDaysFromNow = [calendar dateByAddingUnit:NSCalendarUnitDay value: 7 toDate: startDate options: NSCalendarMatchNextTime];
NSLog(@"%@", sevenDaysFromNow);

Upvotes: 0

Ketan P
Ketan P

Reputation: 4379

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 !!!

Upvotes: 3

Muhammad Salman
Muhammad Salman

Reputation: 553

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];

NSLog(@"nextDate: %@ ...", nextDate);

From this you can calculate next day mean if today is 30 then the next day is 1 so you can use for loop and run it seven times than you get the next seven days

Upvotes: 0

Related Questions