-2

I am receiving time in seconds from server and then i am converting tjose seconds into month using this code:

NSDateFormatter * dateFormatter=[[[NSDateFormatter alloc]init]autorelease];
dateFormatter setDateFormat:@"MMM"];
[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeStamp]];

The problem is I dont get every month from the server like if I am getting Jan first then second time I'm getting Apr from server but I need to show feb and Mar (missing month) on my iPhone screen (Server people cannot change their code so I have to do it at my end).
Can anyone suggest me how can I get missing months.
The same problem is sometimes I get Nov and then Feb from server, but I also have to show Dec and Jan.
Any help will be appreciated!

Niklas
  • 13,005
  • 23
  • 79
  • 119
Anshul
  • 279
  • 4
  • 12
  • format your code please, it is completely unreadable – Vladimir Oct 15 '12 at 11:22
  • NSDateFormatter * dateFormatter=[[[NSDateFormatter alloc]init]autorelease]; dateFormatter setDateFormat:@"MMM"]; [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeStamp]]; – Anshul Oct 15 '12 at 11:25
  • You should check the data from your server. For me the little bit code your posted looks good. – AlexVogel Oct 15 '12 at 12:06
  • the code is fine i need a way to check which month i am missing and how to show them – Anshul Oct 15 '12 at 12:15

1 Answers1

0

You can compare for the difference between two dates as timestamps

NSDateFormatter * dateFormatter=[[[NSDateFormatter alloc]init]autorelease];
dateFormatter setDateFormat:@"MMM"];
NSString* monthName = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:newTimestamp]]

NSTimeInterval month = 60.0 * 60.0 * 24.0 * 31.0; //seconds * minutes * hours * days ~ month
NSTimeInterval tempTimestamp = oldTimestamp;
while(tempTimestamp - newTimestamp) > month){
    //more than a month difference -> add a month
    NSString* tempMonthName = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:tempTimestamp]];
    if(![monthName isEqualTo:newMonthName]){
        //do whatever you need with the additional month name...
    }
    tempTimestamp += month;
}

Hope it will help.

Cheers...

Ariel
  • 2,430
  • 1
  • 17
  • 20