-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDate+Extensions.m
121 lines (88 loc) · 3.31 KB
/
NSDate+Extensions.m
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// NSDate+Extensions.m
//
#import "NSDate+Extensions.h"
@implementation NSDate (Extensions)
#pragma mark -
#pragma mark Instance Methods
- (NSDate *)getLocalDate
{
NSTimeInterval timezoneOffset;
timezoneOffset = [[NSTimeZone localTimeZone] secondsFromGMT];
return [self dateByAddingTimeInterval:timezoneOffset];
}
- (NSString *)getFormattedDateString
{
NSDateFormatter *dateFormatter;
NSString *dateString;
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
dateString = [dateFormatter stringFromDate:self];
return dateString;
}
- (NSString *)getNiceFormattedDateString
{
NSDateFormatter *dateFormatter;
NSString *dateString;
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, MMM d 'at' h:mm a")];
dateString = [dateFormatter stringFromDate:self];
return dateString;
}
#pragma mark -
#pragma mark Class Methods
+ (NSDate *)getDateFromIso:(NSString *)isoDateString
{
NSDateFormatter *formatter;
NSDate *date;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
date = [formatter dateFromString:isoDateString];
return date;
}
+ (NSDate *)getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
//
int startPos = [dateString rangeOfString:@"("].location+1;
int endPos = [dateString rangeOfString:@")"].location;
NSRange range = NSMakeRange(startPos, endPos - startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSTimeInterval interval = milliseconds / 1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}
+ (int)getUtcOffset
{
int utcOffset;
utcOffset = [[NSTimeZone localTimeZone] secondsFromGMT] / 60;
return utcOffset;
}
+ (NSDate *)mfDateFromDotNetJSONString:(NSString *)string
{
static NSRegularExpression *dateRegEx = nil;
static dispatch_once_t onceToken;
dispatch_once(
&onceToken,
^{
dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil];
});
NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
if (regexResult)
{
// milliseconds
NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0;
// timezone offset
if ([regexResult rangeAtIndex:2].location != NSNotFound)
{
NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]];
// hours
seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0;
// minutes
seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0;
}
return [NSDate dateWithTimeIntervalSince1970:seconds];
}
return nil;
}
@end