区域格式在iOS中无法正常工作
对于手机设置,我意识到,当我的用户使用英国的地区格式时,它将一周的时间范围变为2013年11月10日至2013年11月16日。但对于美国,它显示了正确的周范围,即2013年11月3日至2013年11月9日 有人知道用程序解决问题的正确方法吗 这是我的代码区域格式在iOS中无法正常工作,ios,objective-c,Ios,Objective C,对于手机设置,我意识到,当我的用户使用英国的地区格式时,它将一周的时间范围变为2013年11月10日至2013年11月16日。但对于美国,它显示了正确的周范围,即2013年11月3日至2013年11月9日 有人知道用程序解决问题的正确方法吗 这是我的代码 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *currentWeekFirstDay =
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];
什么是CWHelperTime?哦,对不起,这是我自己编写的获取一周第一天的函数。我刚刚编辑了它@MartinRHave正在寻找一种更简单的方法来获得一周的第一天,这应该可以正常工作。还请注意,在某些地区,星期日是一周的第一天,而在其他地区则是星期一。
[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];