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小时制

相关文章:

  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-25
  • 2021-11-29
  • 2022-12-23
猜你喜欢
  • 2021-08-19
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2022-02-01
相关资源
相似解决方案