在我的应用程序中,想要从日期中获取日期(即Sunday
,Monday
等)。
我的代码如下:
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
//ask for current week
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:currentDate];
//create date on week start
NSDate* weekstart=[calendar dateFromComponents:comps];
//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];
NSLog(@"%@",nextDate); // date is 2013-06-30 18:30:00 +0000
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"EEEE"]; // day, like "Saturday"
//[myFormatter setDateFormat:@"c"]; // day number, like 7 for saturday
NSString *dayOfWeek = [myFormatter stringFromDate:nextDate];
问题
NSLog(@"Today is: %@", dayOfWeek); // Prints Mondays instead of Sunday
}
我该如何解决这个问题?