Saturday, June 2, 2012

iOS Date Formatting

If you've obtained the date as a long, then create a NSDate object in this way:


        NSTimeInterval interval = (double)dateInLong;
        NSDate *theDate = [[NSDate alloc]initWithTimeIntervalSince1970: interval];


Then, create a date formatter:


        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MMM' 'dd','yyyy' 'h':'mm':'ss' 'a"];
        NSString *theDateStr = [dateFormatter stringFromDate: theDate];

Date format:
MMM: Short form of month spelt out (e.g. Jan, Feb, Mar)
dd: Day, with 0 in front if less than 10
yyyy: Year (4 digit)
h: Hour, without 0 in front if less than 10
mm: minute
ss: second
a: AM/PM

1 comment:

  1. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.

    Formatting Journal

    ReplyDelete