开发者

Get an array of dates in a week with reference of current date

Is there a way to get an array 开发者_如何学Cof dates in a week with reference to the current date?


Can you please go through this code,

NSDate *startDate = ...;
NSDate *endDate = ...;
unsigned int unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comps = [gregorian components:unitFlags fromDate:startDate  toDate:endDate  options:0];
int months = [comps month];
int days = [comps day];

This is a simple example from Apple. for more details


Try This its Working Fine With Me . -(NSMutableArray *) getWeekDays{

    NSDate *today = [NSDate date];

    NSCalendar *gregorian = [[NSCalendar alloc]

                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *weekdayComponents =

    [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];

    NSInteger day = [weekdayComponents day];

    NSInteger weekday = [weekdayComponents weekday];

    int i=(int)weekday;
    int start,end;
    if(i==7)
    {
        start=6;
        end=0;
    }
    else if(i==6)
    {

        start=5;
        end=1;
    }

    else if(i==5)
    {

        start=4;
        end=2;
    }

    else if(i==4)
    {

        start=3;
        end=3;
    }
    else if(i==3)
    {

        start=2;
        end=4;
    }


    else if(i==2)
    {

        start=1;
        end=5;
    }

    else if(i==1)
    {

        start=0;
        end=6;
    }


    NSDate *cdate=[NSDate date];
    NSDateFormatter *df=[[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd/MM/yyyy EEEE"];
    NSMutableArray *WeekDatesArr=[[NSMutableArray alloc] init];

    for(start;start>0;start--)
    {
        NSDate *FirstDate=[cdate addTimeInterval:-(86400)*start];
        NSString *str=[df stringFromDate:FirstDate];
        [WeekDatesArr addObject:str];
    }
    for(int i=0;i<=end;i++)
    {
        NSDate *LastDate=[cdate addTimeInterval:(86400)*i];
        NSString *str1=[df stringFromDate:LastDate];
        NSLog(@"Str %@ ",str1);
        [WeekDatesArr addObject:str1];
    }
    NSLog(@"Dates Description in week %@",WeekDatesArr);  
    return WeekDatesArr;
}

Cheers

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜