0

我遇到了一个新类型的问题。我正在制作时间显示“不到一分钟前”的聊天应用程序。像脸书。但问题是当我像 24 小时一样切换我的 iphone 时间格式时,它还没有更新时间。

-(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;

}
4

1 回答 1

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;
}
于 2013-03-12T06:34:36.880 回答