获得系统时间(注意24小时和12小时制)
1 NSDate *senddate=[NSDate date]; 2 NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; 3 [dateformatter setDateFormat:@"YYYYMMddHHmmss"]; 4 NSString *currenttime=[dateformatter stringFromDate:senddate];
上面是24小时制的。如果要改成12小时的H改成小写h.
判断获取系统时间是12小时还是24小时的代码如下:
NSString *formatStringForHours = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale currentLocale]];
NSRange containsA = [formatStringForHours rangeOfString:@"a"];
BOOL hasAMPM = containsA.location != NSNotFound;
//hasAMPM==TURE为12小时制,否则为24小时制
TO BE FIRST,BEST OR DIFFERENT.