Question

I want to get the current & next week dates with day name.

i.e. If today's date is 28-06-2013 (Friday) then I want to get the dates from 23-06-2013(sunday) to 29-06-2013 (Saturday) as the this current week.

for next week I want to get dates from 30-06-2013(sunday) to 06-07-2013 (saturday).

How can I do this?

Thank you,

Was it helpful?

Solution

Here is sample code that does the trick.

-(NSArray*)daysThisWeek
{
     return  [self daysInWeek:0 fromDate:[NSDate date]];
}

-(NSArray*)daysNextWeek
{
    return  [self daysInWeek:1 fromDate:[NSDate date]];
}
-(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //ask for current week
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date];
    //create date on week start
    NSDate* weekstart=[calendar dateFromComponents:comps];
    if (weekOffset>0) {
        NSDateComponents* moveWeeks=[[NSDateComponents alloc] init];
        moveWeeks.week=1;
        weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0];
    }

    //add 7 days
    NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
    for (int i=1; i<=7; i++) {
        NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
        compsToAdd.day=i;
        NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
        [week addObject:nextDate];

    }
    return [NSArray arrayWithArray:week];
}

OTHER TIPS

I was going to copy and paste an answer, but I'd suggest going and reading this post: Current Week Start and End Date. It looks like it'll help you achieve what you want to.

Something like this perhaps:

NSDate *now = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber =  [[calendar components: NSWeekCalendarUnit fromDate:now] week];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeek:weekNumber];  //Week number.

int i;

for (i = 1; i < 8; i=i+1){

    [comp setWeekday:i]; //First day of the week. Change it to 7 to get the last date of the week

    NSDate *resultDate = [gregorian dateFromComponents:comp];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE MMMM d, yyyy"];

    NSString *myDateString = [formatter stringFromDate:resultDate];
    NSLog(@"%@",myDateString);

}

Use NSDateComponents to find out the day of the week and then to find the date required before and after it.

Umm, I'm not an expert on this or anything, but a dumb way to do this is to first be able to recognize the day of the week from NSDate. Then you could add in if statements then the if statements could add and subtract the dates and nslog or printf each of them. Or I think there is a way by accessing the ios calendar.

okay so the above pretty much said and did more than what I said. Yeah go check the link on the post before me.

Use an NSCalendar instance to convert your start NSDate instance into an NSDateComponents instance, add 1 day to the NSDateComponents and use the NSCalendar to convert that back to an NSDate instance. Repeat the last two steps until you reach your end date.

NSDate *currentDate = [NSDate date];
 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
 NSDateComponents *comps = [[NSDateComponents alloc] init];
 [comps setDay:1];
 NSDate *nextDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

Or else find start date and end date. like

NSDate *startDate = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:7];
 NSDate *endDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

then, NSDate nextDate;

for ( nextDate = startDate ; [nextDate compare:endDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) {
  // use date
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top