Skip to content

Instantly share code, notes, and snippets.

@eternalstorms
Created August 29, 2012 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eternalstorms/3513336 to your computer and use it in GitHub Desktop.
Save eternalstorms/3513336 to your computer and use it in GitHub Desktop.
return a well-formatted string from an NSDate, presentable to the user
- (NSString *)stringForDate:(NSDate *)date
withLocale:(NSLocale *)locale
timeZone:(NSTimeZone *)timeZone
dateFormat:(NSString *)dateFormat
dateStyle:(NSDateFormatterStyle)style
{
if (dateFormat == nil)
dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
if (locale == nil)
locale = [NSLocale currentLocale];
if (timeZone == nil)
timeZone = [NSTimeZone systemTimeZone];
NSDateFormatter *fm = [[NSDateFormatter alloc] init];
fm.locale = locale;
fm.timeZone = timeZone;
fm.dateFormat = dateFormat;
fm.dateStyle = style;
fm.timeStyle = style;
NSString *str = [fm stringFromDate:date];
[fm release];
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment