NSDateFormatter

NSDateFormatter* dateFormatter = [[[NSDateFormatteralloc]init]autorelease];
 

[dateFormatter setAMSymbol:@"AM"];

[dateFormatter setPMSymbol:@"PM"];

[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];

NSDate*date = [NSDatedate];

NSString* s = [dateFormatterstringFromDate:date];

显示效果为:10/05/201003:49PM

下面附上几个格式:这个是从Cocoachina上来的。呵呵。

yyyy:MM:dd G'at'HH:mm:ss zzz           1996.07.10AD at15:08:56PDT

EEE, MMM d,"yy                                         Wed,july 10, '99

h:mm a                                                     12:08 PM

hh 'o"clock' a,zzzz                                      12 o'clock PM, Pacific Daylight Time

K:mm a, z                                                0:00PM, PST

yyyyy,MMMM.dd GGG hh:mm aaa               01996.july.10AD12:08PM



HH 24小时制,hh 12小时制

下面是得到当前的年,月,日,时,分,秒。

NSCalendar *cal = [NSCalendar currentCalendar];

unsignedintunitFlags =NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;

NSDateComponents*dd = [cal components:unitFlags fromDate:date]; 

inty = [ddyear];

intm = [ddmonth];

intd = [ddday];

inth = [ddhour];

intm = [dd minute];

ints = [dd second];

 

+(NSDate *)NSStringDateToNSDate:(NSString *)string {    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    //#define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd'T'HH:mm:ss'Z'")
    [formatter setDateFormat:kDEFAULT_DATE_TIME_FORMAT];
    NSDate *date = [formatter dateFromString:string];
    [formatter release];
    return date;
}


你可能感兴趣的:(NSDateFormatter)