-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSDate+Additions.m
67 lines (50 loc) · 1.68 KB
/
NSDate+Additions.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
//
// NSDate+Additions.m
// Additions
//
// Created by jlopez on 2/27/10.
// Copyright 2010 JLA. All rights reserved.
//
#import "NSDate+Additions.h"
#import "ISO8601DateFormatter.h"
@implementation NSDate (JLAdditions)
static ISO8601DateFormatter *iso8601Formatter;
+ (ISO8601DateFormatter *)iso8601Formatter {
if (iso8601Formatter == nil) {
iso8601Formatter = [ISO8601DateFormatter new];
iso8601Formatter.parsesStrictly = YES;
iso8601Formatter.includeTime = YES;
}
return iso8601Formatter;
}
+ (NSDate *)dateFromISO8601String:(NSString *)iso8601 {
return [[self iso8601Formatter] dateFromString:iso8601];
}
- (NSString *)iso8601String {
return [[NSDate iso8601Formatter] stringFromDate:self];
}
- (NSString *)shortStringWithMeridian:(BOOL *)isPM {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:self];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
if (isPM)
*isPM = hour >= 12;
hour %= 12;
if (hour == 0)
hour = 12;
if (minute == 0)
return [NSString stringWithFormat:@"%d", hour];
return [NSString stringWithFormat:@"%d:%02d", hour, minute];
}
- (NSDate *)dateByTruncatingTime {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:self];
return [[NSCalendar currentCalendar] dateFromComponents:components];
}
- (NSString *)stringWithFormat:(NSString *)format {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:format];
NSString *s = [formatter stringFromDate:self];
[formatter release];
return s;
}
@end