Get missing month from timestamp Interval

前端 未结 1 1973
旧巷少年郎
旧巷少年郎 2021-01-29 13:02

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

NSDateFormatter * dateFormatter=[[[NSDateFormatte         


        
1条回答
  •  醉梦人生
    2021-01-29 13:27

    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...

    0 讨论(0)
提交回复
热议问题