-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathNSDate+NiceFormats.m
71 lines (55 loc) · 2.05 KB
/
NSDate+NiceFormats.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
#import "NSDate+NiceFormats.h"
#import "NSString+SSYExtraUtils.h"
static NSDateFormatter* static_geekDateFormatter = nil;
static NSDateFormatter* static_geekMilliDateFormatter = nil;
@implementation NSDate (NiceFormats)
- (NSString*)medDateShortTimeString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateStyle:NSDateFormatterMediumStyle] ;
[dateFormatter setTimeStyle:NSDateFormatterShortStyle] ;
NSString* string = [dateFormatter stringFromDate:self];
#if !__has_feature(objc_arc)
[dateFormatter release];
#endif
return string ;
}
+ (NSDateFormatter*)geekDateFormatter {
if (!static_geekDateFormatter) {
static_geekDateFormatter = [[NSDateFormatter alloc] init] ;
[static_geekDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"] ;
}
return static_geekDateFormatter ;
}
+ (NSDateFormatter*)geekMilliDateFormatter {
if (!static_geekMilliDateFormatter) {
static_geekMilliDateFormatter = [[NSDateFormatter alloc] init] ;
[static_geekMilliDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"] ;
}
return static_geekMilliDateFormatter ;
}
- (NSString*)geekDateTimeString {
return [[[self class] geekDateFormatter] stringFromDate:self] ;
}
- (NSString*)geekDateTimeStringMilli {
return [[[self class] geekMilliDateFormatter] stringFromDate:self] ;
}
- (NSString*)hourMinuteSecond {
return [[self geekDateTimeString] substringFromIndex:11] ;
}
- (NSString*)compactDateTimeString {
// Remove spaces, dashs and colons from YYYY-MM-DD HH:MM:SS
NSString* s1 = [[self geekDateTimeString] stringByReplacingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" :-"]
withString:@""] ;
return s1 ;
}
+ (NSString*)currentDateFormattedConcisely {
return [[NSDate date] medDateShortTimeString] ;
}
/*
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ss.SSSS"];
NSDate *date = [NSDate date];
NSString* secondsWithMilliseconds = [dateFormatter stringFromDate:date];
[dateFormatter release] ;
*/
@end