12

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date rolls over from December 26th to December 27th, the year changes to 2010.

I even rolled it forward to 2011... and it changes when December 25th changes to the 26th.... but wait... in 2012, it correctly rolls over on December 31 - January 1... and then its back to 29th-30th in 2013. Is there some kind of astronomical phenomenon I am not aware of going on or does Apple run on some crazy Heechee calendar I don't know of? The calendar app works correctly...

The most likely explanation is I am missing something so obvious that I will slap myself when I realize it. But hey, I haven't slept in... wow I don't remember if its been two days or three. Take pity and help me out here.

UPDATE: So maybe it wasn't something simple. Still looking for an answer here! Am I really the only person who has experienced this?? I'll bet when the end of December rolls around, more people will hit the same roadblock.

UPDATE2: Anyone? Still looking, still not finding...

UPDATE3: Still haven't found a solution. Come on! This is an interesting puzzle!

UPDATE4: Still no answer found. I have submitted the app and it is now in the appstore. Would still like to see this problem solved so I can update the app.

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Taylor
  • 240
  • 1
  • 10

3 Answers3

3

There may be this problem, that when you are on the last week of the month and the week has fewer than 7 days left in current month, then perhaps the API treated the week as the first week of the next month. Since december of 2012 has already 7 days in its last week there is no problem in that month.

I was getting the same problem here, and I solved it.

- (int) currentWeekOfMonth
{
    return CFCalendarGetOrdinalityOfUnit (
           CFCalendarCopyCurrent(),
           kCFCalendarUnitWeek,
           kCFCalendarUnitMonth,
           [self absoluteTime]);
}

my requirement is to show week number and for this i calculate the week number of first week of month and the add this to the total number of week in month.

int currentWeekNumberInYear =   [_calendarInfo currentWeekOfYear];
int currentWeekNumberInMonth = [_calendarInfo currentWeekOfMonth];
currentWeekNumberInYear = currentWeekNumberInYear-currentWeekNumberInMonth +1;
currentWeekNumberInYear = currentWeekNumberInYear<0 ? (NSInteger)[_calendarInfo weeksInMonth] ==5 ?49:48   : currentWeekNumberInYear;

I hope it will be useful to you.

John Rudy
  • 37,282
  • 14
  • 64
  • 100
vikas
  • 827
  • 1
  • 9
  • 19
3

Turns out its the Date format string used to set up the NSDateFormatter that was causing this for me.

"yyyy" corresponds to the 4-digit year, while YYYY corresponds to the year starting from the Sunday of the 1st week of year. Why anyone would want this is anyone's guess, and it would really help if Apple provided a link to their list of format specifiers, but there you go.

Just make sure your format string has the year component in lowercase and it should be sorted.

Graham
  • 31
  • 1
1

This post on TUAW describes a similar problem in PhotoBooth on Mac OS X:

http://www.tuaw.com/2009/12/29/beware-photo-booth-time-stamps-its-a-bug-not-a-feature/

One commenter agrees with vikas that it's an end-of-week issue.

Frank Schmitt
  • 25,648
  • 10
  • 58
  • 70