Created
July 25, 2015 14:38
-
-
Save TheFinestArtist/1ecce2baaf35eed57ce1 to your computer and use it in GitHub Desktop.
NSDate+Custom
This file contains hidden or 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
// | |
// NSDate+Custom.h | |
// | |
// Created by TheFinestArtist | |
// | |
#import <Foundation/Foundation.h> | |
#define DATE_FORMAT_SERVER @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" | |
#define DATE_FORMAT_POST @"yy/MM/dd HH:mm" | |
#define DATE_FORMAT_WHOLE @"yyyy/MM/dd HH:mm" | |
#define DATE_FORMAT_DATE @"yyyy/MM/dd" | |
#define DATE_FORMAT_TIME @"HH:mm" | |
@interface NSDate (Custom) | |
+ (NSDate *)dateFromServerString:(NSString *)string; | |
+ (NSString *)stringWithDate:(NSDate *) date withFormat:(NSString *)format; | |
@end |
This file contains hidden or 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
// | |
// NSDate+Custom.m | |
// | |
// Created by TheFinestArtist | |
// | |
#import "NSDate+Custom.h" | |
@implementation NSDate (Custom) | |
+ (NSDate *)dateFromServerString:(NSString *)string { | |
if (string == nil || string == (NSString *) [NSNull null] || string.length == 0) | |
return nil; | |
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; | |
[dateformatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; | |
[dateformatter setDateFormat:DATE_FORMAT_SERVER]; | |
return [dateformatter dateFromString:string]; | |
} | |
+ (NSString *)stringWithDate:(NSDate *) date withFormat:(NSString *)format { | |
if (date == nil) | |
return nil; | |
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init]; | |
[dateformatter setTimeZone:[NSTimeZone localTimeZone]]; | |
[dateformatter setDateFormat:format]; | |
return [dateformatter stringFromDate:date]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment