在iPhone中查找周数

时间:2011-08-19 12:35:54

标签: iphone objective-c nsdate

这一定很简单,但我无法弄明白。

我想在iPhone中找到周数作为整数

这显示了如何获取String Code

-(NSString*) getWeekOfMonth {
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
 [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
 [dateFormatter setDateFormat:@"W"];

 NSString *weekNumber = [dateFormatter stringFromDate:[NSDate date]];    
 [dateFormatter release];    
 return weekNumber;
}

由于

3 个答案:

答案 0 :(得分:1)

使用integerValue

NSInteger weekInt = [weekNumber integerValue];

答案 1 :(得分:0)

int intWeekNumber = [weekNumber intValue];

答案 2 :(得分:0)

或使用:

CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef currentTimeZone = CFTimeZoneCopyDefault();    
SInt32 weekNumber = CFAbsoluteTimeGetWeekOfYear(currentTime, currentTimeZone);