Yesterday I experienced a strange behaviour while I was using NSDateFormatter.

I had the need to read data from an sqlite database starting from 2012-12-31 ending to today.

So I prepared the date to insert into the sql query in the following way:

 

– (void) myMethod:(NSDate*)start endDate:(NSDate*)end

{

    NSDateFormatter *formatter        =  [[NSDateFormatter alloc] init];

    NSDateFormatter *formatter1       =  [[NSDateFormatter alloc] init];

    NSString        *startDate;

    NSString        *endDate;

    

    [formatter setDateFormat:@"YYYY MM dd"];

    [formatter1 setDateFormat:@"YYYY MM dd"];

    startDate        = [formatter stringFromDate:start];

    endDate          = [formatter1 stringFromDate:end];


    NSLog(@"***********************");

    NSLog(@"startDate = %@", startDate);

    NSLog(@"endDate   = %@", endDate);

    NSLog(@"***********************");

}

And the result was

 

MyApp[4754:3079] -[CViewController myMethod] [Line 234] ***********************

MyApp[4754:3079] -[CViewController myMethod] [Line 235] startDate = 2013 12 31

MyApp[4754:3079] -[CViewController myMethod] [Line 236] endDate   = 2013 02 03

MyApp[4754:3079] -[CViewController myMethod] [Line 237] ***********************

And the startDate was terribly wrong!!!!!!!!!!

If would have read the manual I have done the work in half time.

iossdk

From the Apple Online Docs:

"There are two things to note about this example:

It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm."

So the code must be

 

 

– (void) myMethod:(NSDate*)start endDate:(NSDate*)end

{

    NSDateFormatter *formatter        =  [[NSDateFormatter allocinit];

    NSDateFormatter *formatter1       =  [[NSDateFormatter allocinit];

    NSString        *startDate;

    NSString        *endDate;

    

    [formatter setDateFormat:@"yyyy MM dd"];

    [formatter1 setDateFormat:@"yyyy MM dd"];

    startDate        = [formatter stringFromDate:start];

    endDate          = [formatter1 stringFromDate:end];


    NSLog(@"***********************");

    NSLog(@"startDate = %@", startDate);

    NSLog(@"endDate   = %@", endDate);

    NSLog(@"***********************");

}

 

And the new result:

 

 

MyApp[4754:3079] -[CViewController myMethod] [Line 234] ***********************

MyApp[4754:3079] -[CViewController myMethod] [Line 235] startDate = 2012 12 31

MyApp[4754:3079] -[CViewController myMethod] [Line 236] endDate   = 2013 02 03

MyApp[4754:3079] -[CViewController myMethod] [Line 237] ***********************

 

My old chief said to me "RTFM". Next time I'll follow his advice, I will Read The Fu__ing Manual befor.

Gg1