Joined November 2012
·
Portrait sam

Samuel Chow

Engineering (Mobile and Devops) at Agralogics
Engineering (Mobile and Devops)
Agralogics
·
Santa Cruz, CA, USA
·
·
·

That's a good point. I usually instantiate NSDateFormatter as a static variable especially if I know that the date format is static and pre-defined.

NSString *getDateStringFromDate(NSDate *date) {
  static NSDateFormatter *dateFormatter = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
     dateFormatter = [[NSDateFormatter alloc] init];
     // Output: 2011-05-01 13:15:08
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
  });

   return [dateFormatter stringFromDate:date];
 }
Achievements
798 Karma
79,438 Total ProTip Views