Apple Objective-C programming fundamentals. Working with Dates

When you need to display date and time on your app’s screen you need an easy way to convert NSDate objects to NSString objects. Objective-C  contains methods to do the conversion, but the syntax is not simple. It is much easier to create a method that does the conversion and call that in a central place.

Create a method to convert NSDate objects to NSString using the current date format of the device.

Add a new class to the project

  • Click the + sign in the lower left corner of the Project Navigator
  • Select New File… in the pop-up menu
  • Select IOS -> Cocoa Touch -> Objective-C class
  • Click the Next button
  • Enter CommonMenthods into the Class field
  • Select NSObject in the Subclass of list
  • Click the Next button
  • Click the Create button to save the file in the project folder

Add three method signatures to the CommonMethods class

  • Open the CommonMethods.h file
  • Insert the following code into the @interface section
    + (NSString *)getDateShortString:(NSDate *)date;
    + (NSString *)getTimeShortString:(NSDate *)date;
    + (NSString *)getDateAndTimeShortString:(NSDate *)date;
    
    

Add the three methods to the implementation file

  • Open the CommonMethods.m file
  • Insert the following code into the@implementation section of the file
    // Returns the short date as a string
    + (NSString *)getDateShortString:(NSDate *)date {
        
        NSString *stringFromDate = [NSDateFormatter localizedStringFromDate:date
                                                                  dateStyle:NSDateFormatterShortStyle
                                                                  timeStyle:NSDateFormatterNoStyle];
        
        return stringFromDate;
    }
    
    // Returns the short time as a string
    + (NSString *)getTimeShortString:(NSDate *)date {
        
        NSString *stringFromDate = [NSDateFormatter localizedStringFromDate:date
                                                                  dateStyle:NSDateFormatterNoStyle
                                                                  timeStyle:NSDateFormatterShortStyle];
        
        return stringFromDate;
    }
    
    
    // Returns the short date and time as a string
    + (NSString *)getDateAndTimeShortString:(NSDate *)date {
        
        NSString *stringFromDate = [NSDateFormatter localizedStringFromDate:date
                                                                  dateStyle:NSDateFormatterShortStyle
                                                                  timeStyle:NSDateFormatterShortStyle];
        
        return stringFromDate;
    }
    

To use the new methods

Make the methods available in the class

  • Import the CommonMethods.h file into the class where you want to use the method
  • Insert the following line under the existing #import statement
    #import "lpCommonMethods.h"

Call the methods

  • Use the following syntax to display the date and time in short format set by the user
    self.startDateLabel.text = [CommonMethods getDateAndTimeShortString:self.startDate];

    where
    startDateLabel is a label on the view
    startDate is the NSDate object

Leave a Reply