2013-03-12 69 views
0

我正处于一种新型问题。我正在制作聊天应用程序,时间显示在“不到一分钟前”。像脸书。但问题是,当我切换我的iPhone时间格式,如24小时,它没有更新的时间。如何更新iphone中发布的聊天时间sdk

-(NSString *)calculateTimeSpend:(NSString *)serCurTime andMessagePostTime:(NSString *)serPostTime 





{ 
    actTime = 0;` 
    strTime = NULL; 



    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; 

    NSDate *serTime = [formatter dateFromString:serCurTime]; 
    NSDate *postTime = [formatter dateFromString:serPostTime]; 

    NSTimeInterval interval = [serTime timeIntervalSinceDate:postTime]; 



    //NSLog(@"server - %@, post - %@, interval - %f", serCurTime, serPostTime, interval); 
    [formatter release]; 
    NSString *actualTime = NULL; 
    if (interval<60) 
    { 
     //actTime = interval; 
     //strTime = @"sec(s)"; 
     actualTime = @"Less a min ago."; 
     return actualTime; 
    } 
    else if (interval < 3600) 
    { 
     actTime = trunc(interval/60); 
     strTime = @"min(s)"; 
    } 
    else if (interval < 86400) 
    { 
     actTime = trunc(interval/3600); 
     strTime = @"hour(s)"; 
    } 
    else 
    { 
     actTime = trunc(interval/86400); 
     strTime = @"day(s)"; 
    } 

actualTime = [NSString stringWithFormat:@"%i %@ ago.", actTime, strTime]; 
return actualTime; 

} 
+0

实际重量ü希望? – iPatel 2013-03-12 06:34:13

回答

0

请试试这个方法:

-(NSString *)durationbetweendate:(NSString *)strdate1 date2:(NSString *)strdate2{ 
NSString *stringtime = strdate1; 
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; 


NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; 
[dateFormat1 setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; 
NSDate *sourceDate1 = [dateFormat1 dateFromString:strdate1]; 


NSLog(@"%@",[NSDate date]); 

NSTimeInterval timeDiff ; 

if ([[NSDate date] laterDate:sourceDate1]) 
    timeDiff = [[NSDate date] timeIntervalSinceDate:sourceDate1]; 
else 
    timeDiff = [sourceDate1 timeIntervalSinceDate:[NSDate date]]; 

NSLog(@"second %f",timeDiff); 

float years = floor(timeDiff/(24*60*60*365)); 

if (years >0) { 
    if (years == 1) 
     return [NSString stringWithFormat:@"%d Year ago", (int)years]; 
    else 
     return [NSString stringWithFormat:@"%d Years ago", (int)years]; 
} 
else{ 

    float months = floor(timeDiff/(24*60*60*30)); 

    if (months>0) { 
     if (months == 1) 
      return [NSString stringWithFormat:@"%d Month ago", (int)months]; 
     else 
      return [NSString stringWithFormat:@"%d Months ago", (int)months]; 
    } 
    else{ 

     float days = floor(timeDiff/(24*60*60)); 

     if (days>0) { 
      if (days == 1) 
       return [NSString stringWithFormat:@"%d Day ago", (int)days]; 
      else 
       return [NSString stringWithFormat:@"%d Days ago", (int)days]; 
     } 
     else{ 
      float hours = floor(timeDiff/(60*60)); 

      if (hours>0) { 
       float minutes = 0; 

       minutes = floor(timeDiff/60) - (hours * 3600); 

       float seconds = 0; 

       seconds = round(timeDiff - (minutes * 60) - (hours * 3600)); 

       if (hours==1) 
        return [NSString stringWithFormat:@"%d Hour ago", (int)hours]; 
       else 
        return [NSString stringWithFormat:@"%d Hours ago", (int)hours]; 

      } 
      else 
      { 
       float minutes = floor(timeDiff/60); 

       if (minutes >0) 
       { 
        if (minutes == 1) 
         return [NSString stringWithFormat:@"%d Minute ago", (int)minutes]; 
        else 
         return [NSString stringWithFormat:@"%d Minutes ago", (int)minutes]; 
       } 
       else 
       { 
        float seconds = round(timeDiff - (minutes * 60)); 

        if (seconds == 1) 
         return [NSString stringWithFormat:@"%d Second ago", (int)seconds]; 
        else 
         return [NSString stringWithFormat:@"%d Seconds ago", (int)seconds]; 


       } 
      } 
     } 
    } 
} 

return stringtime; 
} 
+0

是否适用于格式24小时和12小时? – 2013-03-12 06:37:32

+0

是的,它的工作都是。 – 2013-03-12 06:38:24