代码之家  ›  专栏  ›  技术社区  ›  testing

计算日历周

  •  3
  • testing  · 技术社区  · 14 年前

    如何计算日历周?一年有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);
    

    取自 http://iosdevelopertips.com/cocoa/date-formatter-examples.html

    5 回复  |  直到 7 年前
        1
  •  5
  •   Elfred    14 年前

    使用 NSCalendar NSDateComponents .

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
    NSInteger week = [components week];
    
        2
  •  1
  •   Marco    13 年前

    或使用:

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

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

        3
  •  1
  •   Uli2000    10 年前
    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);
    
        4
  •  0
  •   Max Seelemann    14 年前

    NSDateFormatter 像这样:

    NSDateFormatter *fm = [[NSDateFormatter alloc] initWithDateFormat:@"ww" allowNaturalLanguage:NO];
    NSString *week = [fm stringFromDate: date];
    
        5
  •  0
  •   Nghia Luong    8 年前

    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).
    

    更多详细信息: http://www.epochconverter.com/weeknumbers

    苹果也支持这一点,所以你可以从中找到正确的方法 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/

    ISO-8601 standard :

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

    希望这能有所帮助。