0

对于手机设置,我意识到当我的用户使用英国的区域格式时,它将周范围变为 2013 年 11 月 10 日 - 2013 年 11 月 16 日。但对于美国,它显示正确的周范围是 2013 年 11 月 3 日 - 11 月 9 日2013 年。

有没有人知道以编程方式解决的正确方法?

这是我的一段代码...

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];
4

0 回答 0