Click here to Skip to main content
15,887,676 members
Articles / Mobile Apps / iPhone

Formatting Dates relative to Now – Objective C (iPhone)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (4 votes)
1 Sep 2009Ms-PL 18.9K   8   4
A user friendly dateformatter in objective C

Introduction

Many a times, in an iPhone application, you might need to format the date relative to today. This code snippet will help you do it.

C++
(NSString *) formattedDateRelativeToNow:(NSDate *)date
{
   NSDateFormatter *mdf = [[NSDateFormatter alloc] init];
   [mdf setDateFormat:@"yyyy-MM-dd"];
   NSDate *midnight = [mdf dateFromString:[mdf stringFromDate:date]];
   [mdf release];

   NSInteger dayDiff = (int)[midnight timeIntervalSinceNow] / (60*60*24);
   NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 

   if(dayDiff == 0)
      [dateFormatter setDateFormat:@"'Today, 'h':'mm aaa"];
   else if(dayDiff == -1)
      [dateFormatter setDateFormat:@"'Yesterday, 'h':'mm aaa"];
   else if(dayDiff == -2)
      [dateFormatter setDateFormat:@"MMMM d', Two days ago'"];
   else if(dayDiff > -7 && dayDiff <= -2)
      [dateFormatter setDateFormat:@"MMMM d', This week'"];
   else if(dayDiff > -14 && dayDiff <= -7)
      [dateFormatter setDateFormat:@"MMMM d'; Last week'"];
   else if(dayDiff >= -60 && dayDiff <= -30)
      [dateFormatter setDateFormat:@"MMMM d'; Last month'"];
   else if(dayDiff >= -90 && dayDiff <= -60)
      [dateFormatter setDateFormat:@"MMMM d'; Within last three months'"];
   else if(dayDiff >= -180 && dayDiff <= -90)
      [dateFormatter setDateFormat:@"MMMM d'; Within last six months'"];
   else if(dayDiff >= -365 && dayDiff <= -180)
      [dateFormatter setDateFormat:@"MMMM d, YYYY'; Within this year'"];
   else if(dayDiff < -365)
      [dateFormatter setDateFormat:@"MMMM d, YYYY'; A long time ago'"];

   return [dateFormatter stringFromDate:date];
} 
This article was originally posted at http://blog.mugunthkumar.com?p=454

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)



Comments and Discussions

 
GeneralVery simple and clear case. Pin
Pavel Gnatyuk1-Oct-10 22:08
Pavel Gnatyuk1-Oct-10 22:08 
GeneralMy vote of 3 Pin
Otten Dennis18-Jul-10 23:41
Otten Dennis18-Jul-10 23:41 
GeneralMy vote of 2 Pin
leow cheah wei19-Jun-10 12:32
professionalleow cheah wei19-Jun-10 12:32 
Rantnaive solution Pin
sx200820-Sep-09 5:10
sx200820-Sep-09 5:10 
There is a problem with this solution.
Lets assume we have now the 2nd July and there was an event 40 days ago.
This is between 30 and 60 days and the code would state "last month".
But that's not correct, the event was in may - the month before last month.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.