рассчитать календарную неделю - PullRequest
3 голосов
/ 01 сентября 2010

как рассчитать календарную неделю? В году 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 ]

5 голосов
/ 01 сентября 2010

Используйте NSCalendar и NSDateComponents.

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:date];
NSInteger week = [components week];
1 голос
/ 28 декабря 2014
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);
1 голос
/ 16 сентября 2011

Или используйте:

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

Нумерация соответствует определению недели в соответствии с ISO 8601.

0 голосов
/ 30 августа 2016

Существует много видов канлендаров.

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

И Apple это поддерживает, так что вы можете найти правильный путь, указанный в https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/

Например, с ISO-8601 standard:

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

Надеюсь, это поможет.

0 голосов
/ 01 сентября 2010

Вы должны использовать NSDateFormatter, например:

NSDateFormatter *fm = [[NSDateFormatter alloc] initWithDateFormat:@"ww" allowNaturalLanguage:NO];
NSString *week = [fm stringFromDate: date];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...