-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNSString+Dates.m
More file actions
93 lines (83 loc) · 3.04 KB
/
NSString+Dates.m
File metadata and controls
93 lines (83 loc) · 3.04 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// NSString+Dates.m
// evisita
//
// Created by Gabi Martelo on 19/11/13.
// Copyright (c) 2013 Startcapps. All rights reserved.
//
#import "NSString+Dates.h"
@implementation NSString (Dates)
/**
* Convert a NSDate to a NSString
*
* @param dat the NSDate to convert
* @return NSString a localized NSString with the given date
*/
+ (NSString *)stringFormattedForDate:(NSDate *)dat
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *locale = [NSLocale currentLocale];
[dateFormatter setLocale:locale];
return [dateFormatter stringFromDate:dat];
}
/**
* Convert a NSDate to a NSString with the desired format
*
* @param dat the NSDate to convert
* @param timeStyle the NSDateFormatterStyle for the Time
* @param dateStyle the NSDateFormatterStyle for the Date
* @return NSString a localized NSString with the given date and format
*/
+ (NSString *)stringFormattedForDate:(NSDate *)dat
timeStyle:(NSDateFormatterStyle)timeStyle dateStyle:(NSDateFormatterStyle)dateStyle
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:timeStyle];
[dateFormatter setDateStyle:dateStyle];
NSLocale *locale = [NSLocale currentLocale];
[dateFormatter setLocale:locale];
return [dateFormatter stringFromDate:dat];
}
/**
* Create a formatted string with an interval of dates. Use a NSLocalizedString to set a separator between dates.
*
* @param startDate the NSDate with the start date
* @param endDate the NSDate with the end date
* @return NSString a localized NSString with the given interval (Example: 24 Oct a 27 Oct)
*/
+ (NSString *)stringWithIntervalFromDate:(NSDate *)startDate toDate:(NSDate *)endDate
{
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"dMMM" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *strStartDate = [dateFormatter stringFromDate:startDate];
NSString *strEndDate = [dateFormatter stringFromDate:endDate];
return [NSString stringWithFormat:@"%@ %@ %@",
strStartDate,
NSLocalizedString(@"kDateSeparator", nil),
strEndDate];
}
@end
@implementation NSDate (Strings)
/**
* Convert a NSString to a NSDate
*
* @param strDate the NSString to convert
* @return NSDate the converted NSDate
*/
+ (NSDate *)dateFromString:(NSString *)strDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
return [dateFormatter dateFromString:strDate];
}
+ (NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
return [dateFormatter dateFromString:strDate];
}
@end