forked from BlakeBoxberger/Nitty
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime+Date.m
More file actions
45 lines (38 loc) · 1.54 KB
/
Time+Date.m
File metadata and controls
45 lines (38 loc) · 1.54 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
NSDateFormatter *timeFormatter;
NSDateFormatter *dateFormatter;
BOOL is24Hour;
static BOOL timeIs24HourFormat() { // Thank you Michael Waterfall from StackOverflow
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24Hour = amRange.location == NSNotFound && pmRange.location == NSNotFound;
[formatter release];
return is24Hour;
}
static void initializeDateFormatters() {
is24Hour = timeIs24HourFormat();
timeFormatter = [[NSDateFormatter alloc] init];
timeFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
if(is24Hour) {
[timeFormatter setLocalizedDateFormatFromTemplate:@"HH:mm"];
}
else {
[timeFormatter setLocalizedDateFormatFromTemplate:@"h:mm"];
}
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocalizedDateFormatFromTemplate:@"MMM dd"];
}
static NSString *getTimeString() {
NSString *timeString = [timeFormatter stringFromDate:[NSDate date]];
if(!is24Hour) {
timeString = [timeString substringToIndex: [timeString length]-3];
}
return timeString;
}
static NSString *getDateString() {
return [dateFormatter stringFromDate:[NSDate date]];
}