显示当前工作日和日期

时间:2014-09-20 15:29:35

标签: ios objective-c nsdate nscalendar

我有点问题。我希望我在本周显示所有日期和日期(因为我写的是第38周(格里高利))。我所拥有的是好的,但它没有显示正确的日子。例如: 本周一,星期一是9月15日,星期日是9月21日。 当我设定一周的第一天时,它将其定为9月14日星期日。

NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:currentDate];
int thisWeeksNumber = currentComps.weekOfYear;
NSLog(@"1  %d", thisWeeksNumber); //This displays the correct week.

[currentComps setWeekday:1];
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:2];
NSDate *secondDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:3];
NSDate *thirdDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:4];
NSDate *fourthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:5];
NSDate *fifthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:6];
NSDate *sixthDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7];
NSDate *seventhDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"yyyy-MM-dd";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:secondDayOfTheWeek];
NSString *thirdStr = [myDateFormatter stringFromDate:thirdDayOfTheWeek];
NSString *fourthStr = [myDateFormatter stringFromDate:fourthDayOfTheWeek];
NSString *fifthStr = [myDateFormatter stringFromDate:fifthDayOfTheWeek];
NSString *sixthStr = [myDateFormatter stringFromDate:sixthDayOfTheWeek];
NSString *seventhStr = [myDateFormatter stringFromDate:seventhDayOfTheWeek];

NSLog(@"\n %@ \n %@ \n %@ \n %@ \n %@ \n %@ \n %@", firstStr, secondStr, thirdStr, fourthStr, fifthStr, sixthStr, seventhStr);

这应该显示9月15日至9月21日,但它显示9月14日(星期日)至9月21日(星期六)。

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果您查看文档,它会完成它应该做的事情:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDateComponents/setWeekday

当您致电setWeekDay时 - 1表示星期日,所以如果您需要从星期一开始,您的第一个电话应为setWeekDay:2

更新

至于第一个连续几天之后,我会建议实现类似的东西(在你的代码中插入这个函数):

- (NSDate*)dateByAddingDays:(NSInteger)days toDate:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps.day = days;

    return [calendar dateByAddingComponents: comps toDate: date options: 0];
}

并使用此功能从您开始一周的那天起添加1-6。基本上替换所有第二天和其他日期的代码:

[currentComps setWeekday:2];
NSDate *secondDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

以下内容:

NSDate *secondDayOfTheWeek = [self dateByAddingDays:1 toDate:firstDayOfTheWeek];
NSDate *thirdOfTheWeek = [self dateByAddingDays:2 toDate:firstDayOfTheWeek];
...