1
有沒有辦法在一週內獲取參考當前日期的日期數組?參考當前日期獲取一週中的日期數組
有沒有辦法在一週內獲取參考當前日期的日期數組?參考當前日期獲取一週中的日期數組
能否請你去通過這個代碼,
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];
這是蘋果公司一個簡單的例子。 for more details
試試這個工作與我很好。 - (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;
}
乾杯
http://stackoverflow.com/questions/5591612/how-do-i-get-weekday-and-or-name-of-month -from-a-nsdate-variable 這可能對你有所幫助。 – doge 2011-06-14 13:33:52