5

how many weeks there are in a year? because i have searched on the web and i found there is in total 52.1 week in a year, but if i do this in Xcode:

NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];

[gregorian setFirstWeekday:2];

NSDate* sourceDate = [NSDate date];

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

NSDate* today = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];

NSDateComponents *todaysComponents = [gregorian components:NSWeekCalendarUnit fromDate:today];

NSUInteger todaysWeek = [todaysComponents week];

the todaysWeek value is: 53, how it's possible? why is not 52?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • Not the wisest way but this may help you: http://stackoverflow.com/questions/8716649/objective-c-nscalendar-datefromcomponents-not-handling-week-53 – Akshay Shah Dec 24 '12 at 08:55

2 Answers2

1

They converted 52.1 to 53... Its better to show more than to truncate the decimal part.

It also depends on the starting day of the week.

Europe and ISO Standard (from Monday to Sunday) -- 52 full weeks, a short (1 day) week to account for Sunday, January 1st, and a short (1 day) week to account for Monday, December 31st, for a total of 54 weeks.

U.S. Standard (from Sunday to Saturday) -- 52 full weeks, and a short (2 day) week to account for December 30th and 31st, for a total of 53 weeks.

From Saturday to Friday -- 51 full weeks, a short (6 day) week to account for January 1st through January 6th, and a short (3 day) week to account for December 29th through December 31st, for a total of 53 weeks.

EDIT:

Since iOS NSCalender dosent seem to be defining accordingly to ISO 8601 as default. You need to set your calender with method "setMinimumDaysInFirstWeek" to get expected week number result.

Please add this code and check

    [gregorian setMinimumDaysInFirstWeek:5]; //"5" means Thursday in this case.

http://en.wikipedia.org/wiki/ISO_week_date "The first week of a year is the week that contains the first Thursday of the year."

EDIT 2:

For checking the minimum/maximum number of weeks possible in a year

NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
int minWeeks, maxWeek;
minWeeks=maxWeek=0;
for (int weekCounter=1; weekCounter<=7; weekCounter++) {
    [gregorian setMinimumDaysInFirstWeek:weekCounter];
    [gregorian setFirstWeekday:2];
    NSDate* sourceDate = [NSDate date];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* today = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
    NSDateComponents *todaysComponents = [gregorian components:NSWeekCalendarUnit fromDate:today];
    NSUInteger todaysWeek = [todaysComponents week];

    if (todaysWeek>maxWeek) {
        maxWeek=todaysWeek;
    }
    if (todaysWeek<minWeeks || minWeeks==0) {
        minWeeks=todaysWeek;
    }
}
NSLog(@"min:%d, max:%d", minWeeks, maxWeek);
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Now read the full answer, that was incomplete...I was still compiling the answer :) – Anoop Vaidya Dec 24 '12 at 09:08
  • i have delete the previous comment, but this link, don't talk about a 54 week, i think there isn't a case of 54 weeks...http://en.wikipedia.org/wiki/ISO_week_date – Piero Dec 24 '12 at 09:09
  • What is `interval` ? from where and what value are you putting for it? is it NSTimeIntervalSince1970 – Anoop Vaidya Dec 24 '12 at 09:29
  • i'm sorry, now it's correct, i use it to set the hour minutes of the date to zero... – Piero Dec 24 '12 at 09:32
  • I checked your code and the answer is same as in my code posted in the answer... i am attaching screenshot in the answer. – Anoop Vaidya Dec 24 '12 at 09:47
  • i see that you are using a mac os x, but i use iOS iphone, because i have 53 with that code... – Piero Dec 24 '12 at 09:50
  • Whats your localization settings in mac or simulator..? – Anoop Vaidya Dec 24 '12 at 09:53
  • thanks for the info, I checked in both mac and ios. In ios for firstWeekDay=2, it gives 53, and for rest it gives 52. whereas in mac it gives 51 for 3&4 and 52 for other firstWeekDay. I will get back to you soon... – Anoop Vaidya Dec 24 '12 at 10:00
  • thank you very much, now it's works! but works also for the different first weekday? for example in USA the firstweekday is the sunday, the [gregorian setMinimumDaysInFirstWeek:5]; works also there? or in other country? – Piero Dec 24 '12 at 10:48
  • yes...check the calender of Jan 2012...5th is the first thursday. – Anoop Vaidya Dec 24 '12 at 10:57
  • ok thank you very much, very last last question, so in this way i can detect also the year with the 53 weeks right? – Piero Dec 24 '12 at 10:57
  • as already in the answer, people calculate for fiscal, financial, holidays leave policy etc based on some criteria. Like starting day of week and all. And making thursday as the first weekday means you are averaging the week count. – Anoop Vaidya Dec 24 '12 at 11:02
  • i understand, i have only one question and then i accept the answer :), there is a way to know if there is 52 week or 53 in a year, i'm mean, if i found 52, there is a way to know that in that year there ins't another week? and the 52 is the last? – Piero Dec 24 '12 at 11:04
  • Merry Christmas to you man :). And 1 VoteUp for me, and one for you from my side for asking a good question... even I learnt a lot. thnks buddy. Cheers – Anoop Vaidya Dec 24 '12 at 11:15
  • thanks :), i have tried that code for max week in a year, but for this year give me min 52, max 53 what means? that this year we have 53 week? – Piero Dec 24 '12 at 11:20
  • ok thanks! because in this answer http://stackoverflow.com/a/8744594/678833, the 2012 doens't have 53 weeks... – Piero Dec 24 '12 at 11:25
0

Can you try following code?

NSDate *date = [NSDate date];
NSDateFormatter *weekFormatter = [[NSDateFormatter alloc] init];
[weekFormatter setDateFormat:@"w"];
NSString *weekDateString = [weekFormatter stringFromDate:date];
NSLog(@"week = %@",weekDateString);
[weekFormatter release];

You can get the week number of year with above code. But the number is based on current calendar and locale.

Sunil Zalavadiya
  • 1,993
  • 25
  • 36