Comme alternative, vous pouvez éviter l'erreur sujette arithmétique calendrier en se basant sur les composants de calendrier que vous pouvez tirer de la différence entre deux dates :
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 clé est la mise en place de l'unité drapeaux - ce qui vous permet de définir les unités de temps vous voulez la date/heure à se décomposer en. Si vous voulez juste des heures, vous devez définir NSHourCalendarUnit, et cette valeur continuera à augmenter au fur et à mesure que vos dates s'éloigneront, car il n'y a pas de plus grande unité pour commencer à incrémenter. Une fois que vous avez vos composants, vous pouvez continuer avec la logique de votre choix, peut-être en modifiant le flux conditionnel de @ alex.
C'est ce que je jetais ensemble:
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"];
}
Cela fonctionne à merveille. Merci! –
Le seul problème avec cette implémentation est qu'il ne fait aucune distinction entre 24 heures par jour et un jour de calendrier. c'est-à-dire si je compare 23h00 et 02h00, la différence devrait être "hier" pas "3 heures auparavant" Regardez dans NSCalendar et sa classe NSDateComponents. – retainCount