In alternativa, è possibile evitare l'errore incline aritmetica calendario facendo affidamento sulle componenti di calendario si può tirare dalla differenza tra due date :
NSDate *nowDate = [[NSDate alloc] init];
NSDate *targetDate = nil; // some other date here of your choosing, obviously nil isn't going to get you very far
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags
fromDate:dateTime
toDate:nowDate options:0];
NSInteger months = [components month];
NSInteger weeks = [components week];
NSInteger days = [components day];
NSInteger hours = [components hour];
NSInteger minutes = [components minute];
la chiave è la messa a punto delle bandiere unità - questo permette di impostare quali unità di tempo si desidera che la data/ora per essere suddiviso in. Se desideri solo ore, imposterai NSHourCalendarUnit e quel valore continuerà ad aumentare man mano che le date si sposteranno ulteriormente, perché non c'è un'unità più grande per iniziare l'incremento.
Una volta ottenuti i componenti, è possibile procedere con la logica prescelta, magari modificando il flusso condizionale di @ alex.
Questo è quello che ho buttato insieme:
if (months > 1) {
// Simple date/time
if (weeks >3) {
// Almost another month - fuzzy
months++;
}
return [NSString stringWithFormat:@"%ld months ago", (long)months];
}
else if (months == 1) {
if (weeks > 3) {
months++;
// Almost 2 months
return [NSString stringWithFormat:@"%ld months ago", (long)months];
}
// approx 1 month
return [NSString stringWithFormat:@"1 month ago"];
}
// Weeks
else if (weeks > 1) {
if (days > 6) {
// Almost another month - fuzzy
weeks++;
}
return [NSString stringWithFormat:@"%ld weeks ago", (long)weeks];
}
else if (weeks == 1 ||
days > 6) {
if (days > 6) {
weeks++;
// Almost 2 weeks
return [NSString stringWithFormat:@"%ld weeks ago", (long)weeks];
}
return [NSString stringWithFormat:@"1 week ago"];
}
// Days
else if (days > 1) {
if (hours > 20) {
days++;
}
return [NSString stringWithFormat:@"%ld days ago", (long)days];
}
else if (days == 1) {
if (hours > 20) {
days++;
return [NSString stringWithFormat:@"%ld days ago", (long)days];
}
return [NSString stringWithFormat:@"1 day ago"];
}
// Hours
else if (hours > 1) {
if (minutes > 50) {
hours++;
}
return [NSString stringWithFormat:@"%ld hours ago", (long)hours];
}
else if (hours == 1) {
if (minutes > 50) {
hours++;
return [NSString stringWithFormat:@"%ld hours ago", (long)hours];
}
return [NSString stringWithFormat:@"1 hour ago"];
}
// Minutes
else if (minutes > 1) {
return [NSString stringWithFormat:@"%ld minutes ago", (long)minutes];
}
else if (minutes == 1) {
return [NSString stringWithFormat:@"1 minute ago"];
}
else if (minutes < 1) {
return [NSString stringWithFormat:@"Just now"];
}
Questo funziona a meraviglia. Grazie! –
L'unico problema con questa implementazione è che non fa distinzione tra 24 ore come un giorno e un giorno di calendario. Ad esempio, se sto confrontando le 23:00 e le 2:00, la differenza dovrebbe essere "ieri" non "3 ore fa" Cercare in NSCalendar e nella sua classe NSDateComponents associata. – retainCount