Saturday, 23 November 2013

Date formatting

There are some key that use for date formatting in objective-c:

For Time:

a (AM, PM)

//For Hour

h (1, 2, …., 12)
hh (01, 02, …., 12)
H (1, 2, …., 23, 24)
HH (01, 02, …, 23, 24)

//For minute

m(0,1,2,….,58,59)
mm(00,01,02,…,58,59)

//For second

s(0,1,2,….,58,59)
ss(00,01,02,…,58,59)

For Date:

d (1, 2, 3, …., 12, ….., 30, 31)
dd (01, 02, 03, ….., 12, …, 30, 31)

For Day:

e (1 to 7 days)
EEE (Sun, Mon, Tue, Wed, Thu, Fri, Sat)
EEEE (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)

For Month:

M (1, 2, …, 12)
MM (01, 02, …, 12)
MMM (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
MMMM (January, February, March, April, May, June, July, August, September, October, November, December)

For Year:

yy (2 Digits year ex. 12)
YYYY (Full Year ex. 2012)

For Timezone:

z (for GMT Abbreviation of timezone)
zzzz (for GMT Name of timezone)

Methods for date to date formatting and conversion: 

//date to date

 -(NSDate *)convertDateToDate:(NSDate *) date  
 {  
    NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];  
    NSDate *newDate = [[[NSDate alloc] init] autorelease];  
    [formatter setDateFormat:@"yyyy-MM-d"];//use your format   
    NSString * strdate = [formatter stringFromDate:date];  
    newDate = [formatter dateFromString:strdate];  
    return newDate;  
 }  

this may return format of date like 2013-01-23

1 comment: