Translate

2012年8月29日 星期三

NSDateFormatter


部分日期格式的总结:
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mmaaa"];
NSDate *date = [NSDate date];
NSString * s = [dateFormatter stringFromDate:date];
显示效果为:10/05/2010 03:49PM

下面附上几个格式:这个是从Cocoachina找来的:
yyyy:MM:dd G 'at' HH:mm:ss zzz            1996.07.10 AD at 15:08:56 PDT
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:00 PM, PST
yyyyy,MMMM.dd GGG hh:mm aaa                01996.july.10 AD 12:08 PM

下面是得到当前的年,月,日,时,分,秒
NSCalendar *cal = [NSCalendar currentCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dd = [cal components:unitFlags fromDate:date];
int y = [dd year];
int m = [dd month];
int d = [dd day];
int h = [dd hour];
int m = [dd minute];
int s = [dd second];
日期格式代表意義
yyyy = 年(4位數), yy = 年(最後2位數),MM = 月(1~12),MMM = 月(英文縮寫),MMMM = 月(英文完整)
K = 時(12小時制),H = 時(24小時制),mm = 分,ss = 秒,aa = 午前/午後(AM/PM)
dd = 日,EEE = 星期(英文縮寫),EEEE = 星期(英文完整)

沒有留言:

張貼留言