因为NSDateformatter无法使用自定义日历,因此适用于以下日历:
NSCalendar *ISO8601 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
[ISO8601 setMinimumDaysInFirstWeek:7];
[ISO8601 setFirstWeekday:2];
如果我们需要一周的年份,
此日历将于2015-04-26 16:00:00翻译为2015-16周。
所以我会得到一个字符串2015-16th week
。
那么如何将字符串2015-16th week
转换为正确的NSDate或DateComponents,所以如果转换这个新的NSDate或dateComponents,它将不会失去正确性?
答案 0 :(得分:0)
- (NSDate *) dateByAddingWeeks:(NSInteger)dWeeks {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [[NSDateComponents alloc] init];
NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitTimeZone;
components = [gregorian components:unitFlags fromDate:self];
components.day = components.day + 7 * dWeeks;
return [gregorian dateFromComponents:components];
}
不确定。你在寻找这种效用函数吗?