blob: 860e1bcf046a1536323fa0ac9d949ffc81a78db5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
//
// TimeAgo.m
// Juick
//
// Created by Vitaly Takmazov on 10.12.2019.
// Copyright © 2019 com.juick. All rights reserved.
//
#import "NSDate+TimeAgo.h"
@implementation NSDate(TimeAgo)
- (NSString *)timeAgo {
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekOfMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond)
fromDate:self
toDate:now
options:0];
if (components.year > 0) {
formatter.allowedUnits = NSCalendarUnitYear;
} else if (components.month > 0) {
formatter.allowedUnits = NSCalendarUnitMonth;
} else if (components.weekOfMonth > 0) {
formatter.allowedUnits = NSCalendarUnitWeekOfMonth;
} else if (components.day > 0) {
formatter.allowedUnits = NSCalendarUnitDay;
} else if (components.hour > 0) {
formatter.allowedUnits = NSCalendarUnitHour;
} else if (components.minute > 0) {
formatter.allowedUnits = NSCalendarUnitMinute;
} else {
formatter.allowedUnits = NSCalendarUnitSecond;
}
NSString *formatString = NSLocalizedString(@"%@ ago", @"Used to say how much time has passed. e.g. '2 hours ago'");
return [NSString stringWithFormat:formatString, [formatter stringFromDateComponents:components]];
}
@end
|