【问题标题】:Get week number and the date for monday and sunday in the current week (Objective-C on iOS) [closed]获取本周的星期一和星期日的星期数和日期(iOS上的Objective-C)[关闭]
【发布时间】:2011-12-03 22:43:08
【问题描述】:

如何获取当前周数和当前周的周一和周日的日期?

【问题讨论】:

  • 为此,您需要阅读上面列出的组件的规格。并且可能会做一些实际的编程。

标签: objective-c nsdate nsdateformatter nsdatecomponents


【解决方案1】:

我不确定这有多有效,因为它主要来自其他答案

快速浏览一下苹果文档

例子:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7

NSDateComponents *components = [gregorian components:NSWeekOfYearCalendarUnit fromDate:today];

NSUInteger weekOfYear = [components weekOfYear];

NSDate *mondaysDate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysDate interval:NULL forDate:today];

NSLog(@"%ld %@", weekOfYear, mondaysDate);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    相关资源
    最近更新 更多