views:

39

answers:

3

hi,

im trying to increase a date and get a formatted output for every month ... it works fine, but the step Dec 2010 to Jan 2011 doesn't work. please have a look at this:

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *lastDate = [[NSDate alloc] init];

for (int i = 0; i < 12; i++) {
    NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init] autorelease];
    [offsetComponents setMonth: 1];
    lastDate = [[gregorian dateByAddingComponents:offsetComponents toDate: lastDate options:0] retain];


    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateFormat:@"MMMM YYYY"];

    NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
    [dateFormatter setLocale:locale];

    NSLog(@"%d: Fromatted Date: %@ from : %@", i, [dateFormatter stringFromDate:lastDate], lastDate);
}

debug output: [Session started at 2010-07-01 11:05:18 +0200.] 0: Fromatted Date: August 2010 from : 2010-08-01 11:05:19 +0200

1: Fromatted Date: September 2010 from : 2010-09-01 11:05:19 +0200

2: Fromatted Date: October 2010 from : 2010-10-01 11:05:19 +0200

3: Fromatted Date: November 2010 from : 2010-11-01 11:05:19 +0100 4: Fromatted Date: December 2010 from : 2010-12-01 11:05:19 +0100

5: Fromatted Date: January 2010 from : 2011-01-01 11:05:19 +0100 <--- WHY is the year 2010 ?!

6: Fromatted Date: February 2011 from : 2011-02-01 11:05:19 +0100

7: Fromatted Date: March 2011 from : 2011-03-01 11:05:19 +0100

8: Fromatted Date: April 2011 from : 2011-04-01 11:05:19 +0200

9: Fromatted Date: May 2011 from : 2011-05-01 11:05:19 +0200

10: Fromatted Date: June 2011 from : 2011-06-01 11:05:19 +0200

11: Fromatted Date: July 2011 from : 2011-07-01 11:05:19 +0200

+2  A: 

Hi Wuhi,

I have put same code into sample application and i didn't face same problem. Here is the o/p for your reference.

2010-07-01 15:42:30.881 WebService[3188:207] 0: Fromatted Date: August 2010 from : 2010-08-01 15:42:30 +0530

2010-07-01 15:42:30.883 WebService[3188:207] 1: Fromatted Date: September 2010 from : 2010-09-01 15:42:30 +0530

2010-07-01 15:42:30.884 WebService[3188:207] 2: Fromatted Date: October 2010 from : 2010-10-01 15:42:30 +0530

2010-07-01 15:42:30.886 WebService[3188:207] 3: Fromatted Date: November 2010 from : 2010-11-01 15:42:30 +0530

2010-07-01 15:42:30.888 WebService[3188:207] 4: Fromatted Date: December 2010 from : 2010-12-01 15:42:30 +0530

2010-07-01 15:42:30.889 WebService[3188:207] 5: Fromatted Date: January 2011 from : 2011-01-01 15:42:30 +0530

2010-07-01 15:42:30.891 WebService[3188:207] 6: Fromatted Date: February 2011 from : 2011-02-01 15:42:30 +0530

2010-07-01 15:42:30.892 WebService[3188:207] 7: Fromatted Date: March 2011 from : 2011-03-01 15:42:30 +0530

2010-07-01 15:42:30.893 WebService[3188:207] 8: Fromatted Date: April 2011 from : 2011-04-01 15:42:30 +0530

2010-07-01 15:42:30.895 WebService[3188:207] 9: Fromatted Date: May 2011 from : 2011-05-01 15:42:30 +0530

2010-07-01 15:42:30.896 WebService[3188:207] 10: Fromatted Date: June 2011 from : 2011-06-01 15:42:30 +0530

2010-07-01 15:42:30.898 WebService[3188:207] 11: Fromatted Date: July 2011 from : 2011-07-01 15:42:30 +0530

Pragnesh Dixit
A: 

hmm :-( really strange. i just made up a new view-based app ... cann't find a hint

**update: ok if i use [dateFormatter setDateFormat: @"MMMM yyyy"] (so just lowercase Y) its working.

y/yyyy: (Full Year)

yy/yyy: (2 Digits Year)

Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)

YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)

wuhi
+1  A: 

Hey Wuhi,

I also didn't face any problem which you are talking about. I also copy your code and was working fine.

I am not sure but i think there must be a problem of SDK versions. because we are using 3.1.3 and was working fine. just try in different sdk versions.

Cheers Mitul

mitul shah