Iphone 计算日历周

Iphone 计算日历周,iphone,objective-c,xcode,calendar,Iphone,Objective C,Xcode,Calendar,如何计算日历周?一年有52/53周,有两条规则: -美国 -DIN 1355/ISO 8601 我想和DIN 1355/ISO 8601一起工作。我该怎么办 编辑: NSDate *today = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"ww"]; NSString *weeknumber = [dateFormat strin

如何计算日历周?一年有52/53周,有两条规则:

-美国

-DIN 1355/ISO 8601

我想和DIN 1355/ISO 8601一起工作。我该怎么办

编辑

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"ww"];
NSString *weeknumber = [dateFormat stringFromDate: today];
NSLog(@"week: %@", weeknumber);
取自


在哪里可以找到允许的日期格式?

您应该像这样使用
NSDateFormatter

NSDateFormatter *fm = [[NSDateFormatter alloc] initWithDateFormat:@"ww" allowNaturalLanguage:NO];
NSString *week = [fm stringFromDate: date];

使用
NSCalendar
NSDateComponents

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
NSInteger week = [components week];
或使用:

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

编号遵循ISO 8601对周的定义。

有许多种日历

NSDate *today = [NSDate date];
NSCalendar *ISO8601 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];
ISO8601.firstWeekday = 2; // Sunday = 1, Saturday = 7
ISO8601.minimumDaysInFirstWeek = 4;
NSDateComponents *components = [ISO8601 components:NSCalendarUnitWeekOfYear fromDate:today];
NSUInteger weekOfYear = [components weekOfYear];
NSDate *mondaysDate = nil;
[ISO8601 rangeOfUnit:NSCalendarUnitYearForWeekOfYear startDate:&mondaysDate interval:NULL forDate:today];
NSLog(@"The current Weeknumber of Year %ld ", weekOfYear);
Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week'). The highest week number in a year is either 52 or 53. This year has 52 weeks.This is not the only week numbering system in the world, other systems use weeks starting on Sunday (US) or Saturday (Islamic).
更多详情:

苹果确实支持这一点,所以你们可以从中找到正确的方法

例如,对于
ISO-8601标准

NSCalendar *ISO8601 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];

希望这能有所帮助。

谢谢您的回复。你的代码是为MacOSX设计的。现在我试着这么做(见编辑的问题),但我得到了35。《展望》第36条。第一个一月也是展望的第一周。差异从何而来?可能取决于本周的开始。在美国,一周的第一天通常是星期天,在欧洲是星期一。也许outlook只提供这两种样式?感谢您提供日期格式的链接。在我读过之后,第一个美国日历周是包含1月1日的一周。今年的星期五是1月1日。因此,在这种情况下,周日或周一是一周的第一天并不重要。我不知道Outlook正在使用什么(它不是ISO,因为第一周应该包含1月4日)。我也不知道苹果的这些课程在做什么。建议?我现在尝试了[cal setFirstWeekday:2];我得到了预期的35分。您发布的链接符合ISO 8601标准(因此新年的四天应该是第一周)日期格式在Apple文档中链接:我必须声明NSDate*date=[NSDate date];仍然存在一个问题。如果是35或36,我如何影响结果。DIN/ISO规定第一周包括1月4日。例如,Outlook指出2010年1月1日、2日、3日(周五至周日)是第一周(美国日历周?)。所以每个地方都有差异,我想根据Outlook得出结果36。日历实际上是基于区域设置的。在美国,它默认为公历。看起来苹果公司正在计划实施ISO8601日历,但还没有完成。他们有一个NSISO8601Calendar密钥来初始化NSCalendar,但文档中说它没有实现。您可以做的一件事是检查1月4日的星期,并根据这一点更改上面的星期。我知道我必须自己做。谢谢你的提示。还有一件事我不明白:苹果是如何计算35的?我看不到任何规则…当你说Outlook时,你是指Microsoft Outlook吗?另外,你知道它使用哪个系统来计数吗。如果你仔细想想,苹果公司似乎正在使用ISO8601计算日期的方法。如果从包括1月4日在内的一周开始计算,这是一年中的第35周。否则,这将是第36周。干得好,欢迎来到Stackoverflow!一般来说,答案中多加一点文字解释您提供的代码是有用的。在这种情况下,你可以把你留下的评论作为解释的一部分,而不是评论。