Tutorial details

Formatting dates using the NSDateFormatter class | App Code for Sale | Preview

Formatting dates using the NSDateFormatter class | iOS Tutorial

How to use NSDateFormatter class

Overview PAGE TOP

One of the most commonly used programming classes when building iOS applications is the NSDate class, which (obviously) allows us to deal with dates. Handling a date is not that hard when it is used internally into a program, but what if we should show a date to the user? Even more, what if we should have the date formatted depending on the app or our needs? How can we be able to transform a date into an NSString and back again?

The answer to all the above questions gives the NSDateFormatter class. Before going into any examples of usage, here are some important things you need to know prior to use it:

There is the setDateStyle method which should be used to set the desired style of date. The provided styles are:

  1. NSDateFormatterNoStyle: No style.
  2. NSDateFormatterShortStyle: 12/18/13
  3. NSDateFormatterMediumStyle: Dec 18, 2013
  4. NSDateFormatterLongStyle: December 18, 2013
  5. NSDateFormatterFullStyle: Wednesday, December 18, 2013

There is also the the setDateFormat method. This one should be used to customize the date in a way other than the default styles. A format string is used to set the appropriate format for the NSDateFormatter using the patterns described here. Here are just some of them (there are many more and you should necessarily check them out, they also regard time formatting):

  • yyyy: Year using four digits, e.g. 2013
  • yy: Year using two digits, e.g. ’13
  • MM: Month using two digits, e.g. 05
  • MMMM: Month, full name, e.g. March
  • dd: Day with two digits, e.g. 14
  • EEEE: Day of the week, full name, e.g. Friday
  • EEE: Day of the week, short, e.g. Mon

For example, the next format string:

EEEE, dd MMMM yyyy

represents the following formatted date (when this quick tutorial was written):

Thursday, 07 March 2013

Let’s see this class in action with some examples:

EXAMPLE 1: CONVERTING THE CURRENT DATE TO AN NSSTRING

1   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
2   [formatter setDateStyle:NSDateFormatterShortStyle];
3   NSDate *dt = [NSDate date];
4   NSString *dateAsString = [formatter stringFromDate:dt];
5   [formatter release];
6    
7   NSLog(@"%@", dateAsString);

Output: 3/7/13

EXAMPLE 2: CONVERTING THE CURRENT DATE TO AN NSSTRING USING CUSTOM FORMAT

1   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
2   [formatter setDateFormat:@"EEE, MM-dd-yyyy"];
3   NSDate *dt = [NSDate date];
4   NSString *dateAsString = [formatter stringFromDate:dt];
5   [formatter release];
6    
7   NSLog(@"%@", dateAsString);

Output: Thu, 03-07-2013

EXAMPLE 3: SETTING A DATE FROM AN NSSTRING VALUE

1   NSString *dateString = @"28042013";
2   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
3    
4   [formatter setDateFormat:@"ddMMyyyy"];
5   NSDate *dt = [formatter dateFromString:dateString];
6    
7   [formatter setDateStyle:NSDateFormatterFullStyle];
8   NSString *dateAsString = [formatter stringFromDate:dt];
9    
10  [formatter release];
11   
12  NSLog(@"%@", dateAsString);

Output: Sunday, April 28, 2013

EXAMPLE 4: SET DATE LOCALE

You can use the setLocale method of the NSDateFormatter class for specific locale.

1   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
2   [formatter setDateFormat:@"EEEE, MMMM dd, yyyy"];
3    
4   NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"el_GR"];
5   [formatter setLocale:locale];
6   [locale release];
7    
8   NSDate *dt = [NSDate date];
9   NSString *dateAsString = [formatter stringFromDate:dt];
10   
11  NSLog(@"%@", dateAsString);
12   
13  locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
14  [formatter setLocale:locale];
15  [locale release];
16   
17  dateAsString = [formatter stringFromDate:dt];
18   
19  [formatter release];
20   
21  NSLog(@"%@", dateAsString);

Output:
Πέμπτη, Μαρτίου 07, 2013
Thursday, March 07, 2013

So, that’s all in short regarding the date formatting in iOS. There are much more to read for sure and that’s what I totally recommend. For a complete reference, here is the Apple documentation:

  • NSDateFormatter Class Reference
  • Date Formatters

Reference PAGE TOP

http://gtiapps.com/?p=1086

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.