Skip to content

Instantly share code, notes, and snippets.

@furkanmustafa
Last active June 13, 2017 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furkanmustafa/6854367 to your computer and use it in GitHub Desktop.
Save furkanmustafa/6854367 to your computer and use it in GitHub Desktop.
a few NSDateFormatter helpers for Objective-C
/* f@s2n.io */
// DateFormat Reference : http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns
#import <Foundation/Foundation.h>
extern NSString* const DATEFORMAT_RFC3339;
@interface NSDate (FMDateFormatters)
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format locale:(NSLocale*)locale timezone:(NSTimeZone*)zone;
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format;
+ (NSDate*)dateWithPosixString:(NSString*)dateString format:(NSString*)format;
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone locale:(NSLocale*)locale;
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone;
- (NSString*)formattedString:(NSString*)format withOptions:(void(^)(NSDateFormatter* formatter))optionsBlock;
- (NSString*)formattedString:(NSString*)format;
- (NSString*)posixFormattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone;
- (NSString*)posixFormattedString:(NSString*)format;
@end
/* f@s2n.io */
#import "NSDate+Formatters.h"
NSString* const DATEFORMAT_RFC3339 = @"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'";
@implementation NSDate (FMDateFormatters)
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone locale:(NSLocale*)locale {
NSDateFormatter *formatter = NSDateFormatter.new.autorelease;
if (locale)
formatter.locale = locale;
formatter.dateFormat = format;
if (zone)
formatter.timeZone = zone;
return [formatter stringFromDate:self];
}
- (NSString*)formattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone {
return [self formattedString:format withTimeZone:zone locale:NSLocale.currentLocale];
}
- (NSString*)formattedString:(NSString*)format withOptions:(void(^)(NSDateFormatter* formatter))optionsBlock {
__block NSDateFormatter *formatter = NSDateFormatter.new.autorelease;
[formatter setDateFormat:format];
optionsBlock(formatter);
return [formatter stringFromDate:self];
}
+ (NSTimeZone*)posixTimeZone {
return [NSTimeZone timeZoneForSecondsFromGMT:0];
}
- (NSString*)formattedString:(NSString*)format {
return [self formattedString:format withTimeZone:nil locale:NSLocale.currentLocale];
}
- (NSString*)posixFormattedString:(NSString*)format withTimeZone:(NSTimeZone*)zone {
NSLocale* posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"].autorelease;
return [self formattedString:format withTimeZone:zone locale:posixLocale];
}
- (NSString*)posixFormattedString:(NSString*)format {
NSLocale* posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"].autorelease;
return [self formattedString:format withTimeZone:self.class.posixTimeZone locale:posixLocale];
}
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format {
return [self dateWithString:dateString format:format locale:NSLocale.currentLocale timezone:nil];
}
+ (NSDate*)dateWithPosixString:(NSString*)dateString format:(NSString*)format {
return [self dateWithString:dateString
format:format
locale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"].autorelease
timezone:self.class.posixTimeZone];
}
+ (NSDate*)dateWithString:(NSString*)dateString format:(NSString*)format locale:(NSLocale*)locale timezone:(NSTimeZone*)zone {
if (!dateString || !format) return nil;
NSDateFormatter *formatter = NSDateFormatter.new.autorelease;
if (locale)
formatter.locale = locale;
formatter.dateFormat = format;
if (zone)
formatter.timeZone = zone;
return [formatter dateFromString:dateString];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment