-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimestamp.m
More file actions
77 lines (55 loc) · 2.32 KB
/
Timestamp.m
File metadata and controls
77 lines (55 loc) · 2.32 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Timestamp.m
// TwitterClient
//
// Created by Jessica Ko on 3/30/14.
// Copyright (c) 2014 Jessica Ko. All rights reserved.
//
#import "Timestamp.h"
@implementation Timestamp
+ (NSDate*)dateWithJSONString:(NSString*)dateStr
{
//NSLog(@"Date -- %@",dateStr);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE MMM d HH:mm:ss ZZZ yyyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Test data object if desired output format is achieved
[dateFormat setDateFormat:@"EEE, MMM d YYYY"];
dateStr = [dateFormat stringFromDate:date];
//NSLog(@"Date -- %@",dateStr);
return date;
}
+ (NSString*)formattedDateWithJSONString:(NSString*)dateStr
{
//Tue Aug 28 21:16:23 +0000 2012
//NSLog(@"Date -- %@",dateStr);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEE MMM d HH:mm:ss ZZZ yyyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
//9:16 PM 28 Aug 2012
[dateFormat setDateFormat:@"h:mm a - d MMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
//NSLog(@"Date -- %@",dateStr);
return dateStr;
}
+ (NSString *)relativeTimeWithTimestamp:(NSDate *)date {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:@"am"];
[dateFormatter setPMSymbol:@"pm"];
NSString* timestamp;
int timeIntervalInHours = (int)[[NSDate date] timeIntervalSinceDate:date] /3600;
int timeIntervalInMinutes = [[NSDate date] timeIntervalSinceDate:date] /60;
if (timeIntervalInMinutes < 60){//less than 15 minutes old
timestamp = [NSString stringWithFormat:@"%dm",timeIntervalInMinutes];
} else if (timeIntervalInHours < 24){//less than 1 day
timestamp = [NSString stringWithFormat:@"%dh",timeIntervalInHours];
} else if (timeIntervalInHours < 8765){//less than a year
[dateFormatter setDateFormat:@"d MMM"];
timestamp = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
} else {//older than a year
[dateFormatter setDateFormat:@"d MMM yyyy"];
timestamp = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
}
return timestamp;
}
@end