2

I'm making an app that creates a timetable. You can create a new one every week of the year. When the app has finished loading, it needs to load up the current week (For example: If it's the 1st of January, week 1 needs to be shown). I use NSDateFormatter to determine what the current week is.

NSDateFormatter

(I was testing this on the 9th of August 2016)

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ww"];

int currentWeek = [[time stringFromDate:[NSDate date]] intValue];

I wanted to check if it was working so I used NSLog.

NSLog(@"%i", currentWeek);

It returned 32.

NSDateComponents

So NSDateFormatter thinks the current week is 32. So far, so good. The app needs to send a push notification to tell the user that a certain period is about to begin. So the app schedules a notification using NSDateComponents.

// Setting the notification's fire date.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setWeekday:3];  // Because it's on a Tuesday
[dateComps setWeekOfYear:currentWeek];  // 32
[dateComps setYear:2016];
[dateComps setHour:12];
[dateComps setMinute:20];
NSDate *theFireDate = [calendar dateFromComponents:dateComps];

// Creates the notification.
UILocalNotification *Alert = [[UILocalNotification alloc] init];
Alert.timeZone = [NSTimeZone defaultTimeZone];
Alert.alertBody = @"This is a message!";
Alert.fireDate = theFireDate;
[[UIApplication sharedApplication] scheduleLocalNotification:Alert];

And I also used NSLog.

NSLog(@"%@", theFireDate);

But it returned 2016-08-02 12:20:00 +0000 which is not the current date. It actually is the current date minus 7 days, or one week earlier. So does this mean that the current week is actually 33 instead of 32, which means NSDateFormatter is wrong? Or is it actually 32 which means NSDateComponents is wrong. And what causes the difference between those two?

3
  • 1
    Try to set locale and timezone to date formatter and then debug again. Commented Aug 9, 2016 at 15:47
  • 1
    Don't ask me why, but with NSCalendar *calendar = [NSCalendar currentCalendar], it works (I have a Gregorian one), but with your alloc/init of it, I encounter the same issue. Seems to be missing a setting somewhere. If I set [calendar setLocale:[NSLocale currentLocale]];, it works. Commented Aug 9, 2016 at 16:03
  • 1
    I agree, it looks like you have a mismatch between timezone, locale and calendar. Commented Aug 9, 2016 at 16:03

1 Answer 1

2

Don't use NSDateFormatter, use NSCalendar which is much more accurate.

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSInteger weekOfYear = [calendar component:NSCalendarUnitWeekOfYear fromDate:[NSDate date]];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.