int sec = diff;//INFO: time in seconds
int a_sec = 1;
int a_min = a_sec * 60;
int an_hour = a_min * 60;
int a_day = an_hour * 24;
int a_month = a_day * 30;
int a_year = a_day * 365;
NSString *text = @"";
if (sec >= a_year)
{
int years = floor(sec/a_year);
text = [NSString stringWithFormat:@"%d year%@ ", years, years > 0 ? @"s" : @""];
sec = sec - (years * a_year);
}
if (sec >= a_month)
{
int months = floor(sec/a_month);
text = [NSString stringWithFormat:@"%@%d month%@ ", text, months, months > 0 ? @"s" : @""];
sec = sec - (months * a_month);
}
if (sec >= a_day)
{
int days = floor(sec/a_day);
text = [NSString stringWithFormat:@"%@%d day%@ ", text, days, days > 0 ? @"s" : @""];
sec = sec - (days * a_day);
}
if (sec >= an_hour)
{
int hours = floor(sec/an_hour);
text = [NSString stringWithFormat:@"%@%d hour%@ ", text, hours, hours > 0 ? @"s" : @""];
sec = sec - (hours * an_hour);
}
if (sec >= a_min)
{
int minutes = floor(sec/a_min);
text = [NSString stringWithFormat:@"%@%d minute%@ ", text, minutes, minutes > 0 ? @"s" : @""];
sec = sec - (minutes * a_min);
}
if (sec >= a_sec)
{
int seconds = floor(sec/a_sec);
text = [NSString stringWithFormat:@"%@%d second%@", text, seconds, seconds > 0 ? @"s" : @""];
}
NSLog(@"<%@>", text);
Sind Sie sicher, dass die Dauer Zahl nicht die Anzahl der Sekunden? Also 2321 ist wirklich 38 Minuten und 41 Sekunden? – rmaddy
Es stellt sich heraus, dass ich richtig liege. Der Wert für die Dauer ist die Anzahl der Sekunden. So 2321 ist 2321 Sekunden oder 38 Minuten und 41 Sekunden oder 38:41. Siehe meine Antwort unten. – rmaddy