我一直在为 iPhone 开发一个天气应用程序,在 iOS5 上一切正常,但在 iOS6 上显示的日子有些奇怪......
但奇怪的是这个代码返回satuarday instead of monday sunday instead of tuesday
等等......但它在iOS 5上运行良好
我正在使用以下方法来显示星期几..
-(NSString*)calculateDayOfTheWeek:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
// [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSDate *date1 = [formatter dateFromString:date];
// NSCalendar *gregorian = [NSCalendar currentCalendar];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:date1];
int w = [comps weekday];
// NSLog(@"%i",w);
if(w == 1)
return @"Sun";
else if (w == 2)
return @"Mon";
else if(w == 3)
return @"Tue";
else if (w == 4)
return @"Wed";
else if(w == 5)
return @"Thu";
else if (w == 6)
return @"Fri";
else if (w == 7)
return @"Sat";
else
return @"";
}