Last active
December 28, 2015 20:49
-
-
Save ishaq/7560292 to your computer and use it in GitHub Desktop.
NSString from NSDate and vice versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSDate (FormattedString) | |
- (NSString *)mi_dateStringWithStyle:(NSDateFormatterStyle)style | |
timeStyle:(NSDateFormatterStyle)timeStyle; | |
- (NSString *)mi_dateStringWithFormat:(NSString *)format; | |
- (NSString *)mi_isoDateString; | |
- (NSString *)mi_ecma262DateString; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSDate+FormattedString.h" | |
@implementation NSDate (FormattedStrings) | |
- (NSString *)mi_dateStringWithStyle:(NSDateFormatterStyle)style | |
timeStyle:(NSDateFormatterStyle)timeStyle { | |
static NSDateFormatter *dateFormatter = nil; | |
if(dateFormatter == nil) | |
{ | |
dateFormatter = [[NSDateFormatter alloc] init]; | |
} | |
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[dateFormatter setDateStyle:style]; | |
[dateFormatter setTimeStyle:timeStyle]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
- (NSString *)mi_dateStringWithFormat:(NSString *)format | |
{ | |
static NSDateFormatter *dateFormatter = nil; | |
if(dateFormatter == nil) | |
{ | |
dateFormatter = [[NSDateFormatter alloc] init]; | |
} | |
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[dateFormatter setDateFormat:format]; | |
return [dateFormatter stringFromDate:self]; | |
} | |
- (NSString *)mi_isoDateString | |
{ | |
return [self dateStringWithFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"]; | |
} | |
- (NSString *)mi_ecma262DateString | |
{ | |
return [self dateStringWithFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HHMM|-HHMM|Z] | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface NSString (DateFormatter) | |
/** | |
* converts the string to date using `format` | |
* | |
* @param format string format | |
* | |
* @return the date corresponding to the string | |
*/ | |
- (NSDate *)mi_dateWithFromat:(NSString *)format; | |
- (NSDate *)mi_dateWithISOString; | |
- (NSDate *)mi_dateWithECMA262String; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSString+DateFormatter.h" | |
@implementation NSString (DateForamtter) | |
- (NSDate *)mi_dateWithFromat:(NSString *)format { | |
static NSDateFormatter *dateFormatter = nil; | |
if(dateFormatter == nil) | |
{ | |
dateFormatter = [[NSDateFormatter alloc] init]; | |
} | |
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[dateFormatter setDateFormat:format]; | |
return [dateFormatter dateFromString:self]; | |
} | |
- (NSDate *)mi_dateWithISOString | |
{ | |
static NSDateFormatter *dateFormatter = nil; | |
if(dateFormatter == nil) | |
{ | |
dateFormatter = [[NSDateFormatter alloc] init]; | |
} | |
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"]; | |
return [dateFormatter dateFromString:self]; | |
} | |
- (NSDate *)mi_dateWithECMA262String | |
{ | |
NSDate *d = [self dateWithFromat:@"YYYY-MM-dd'T'HH:mm:ss.SSSZ"]; | |
if(d == nil) | |
{ | |
d = [self dateWithFromat:@"YYYY-MM-dd'T'HH:mm:ssZ"]; | |
} | |
if(d == nil) | |
{ | |
d = [self dateWithFromat:@"yyyy-MM-dd"]; | |
} | |
return d; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment