对于手机设置,我意识到当我的用户使用英国的地区格式时,它将周范围变为2013年11月10日 - 2013年11月16日。但对于美国,它显示正确的周范围,即2013年11月3日 - 2013年11月9日。
有没有人知道以编程方式解决的正确方法?
这是我的代码......
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentWeekFirstDay = [CWHelperTime getFirstDayOfWeek:[NSDate date]];
if( !self.weekStartDate ){
self.weekStartDate = currentWeekFirstDay;
} else {
NSDateComponents *prevWeekStartComponents = [gregorian components:NSDayCalendarUnit fromDate:self.weekStartDate];
if( self.currentWeekIndex < weekIndex )
prevWeekStartComponents.day = +7;
else
prevWeekStartComponents.day = -7;
self.currentWeekIndex = weekIndex;
self.weekStartDate = [gregorian dateByAddingComponents:prevWeekStartComponents toDate:self.weekStartDate options:0];
}
NSDateComponents *weekEndComponents = [gregorian components:NSDayCalendarUnit fromDate:self.weekStartDate];
weekEndComponents.day = +6;
self.weekEndDate = [gregorian dateByAddingComponents:weekEndComponents toDate:self.weekStartDate options:0];
这是针对getFirstDayOfWeek
的[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Singapore"]];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
// Find Sunday for the given date
components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
[components setWeekday:1]; // 1 == Sunday, 7 == Saturday
[components setWeek:[components week]];
return [calendar dateFromComponents:components];