//实例化一个NSDateFormatter对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
//设定时间格式,这里可以设置成自己需要的格式
[dateFormatter setDateFormatter:@"yyyy-MM-dd HH:mm:ss"];
NSString *currentDate = [dataFormatter stringFromDate:[NSDate date]];
注:这里的 [NSDate date] (当前时间)也可以是 [datePicker date];
输出格式通setDateStyle和setTimeStyle设置,分别定义的日期和时间的格式可选一下的系统给出的方法
typedef enum {
NSDateFormatterNoStyle = kCFDateFormatterNoStyle,
NSDateFormatterShortStyle = kCFDateFormatterShortStyle,//“11/23/37” or “3:30pm”
NSDateFormatterMediumStyle = kCFDateFormatterMediumStyle,//\"Nov 23, 1937\"
NSDateFormatterLongStyle = kCFDateFormatterLongStyle,//\"November 23, 1937” or “3:30:32pm\"
NSDateFormatterFullStyle = kCFDateFormatterFullStyle//“Tuesday, April 12, 1952 AD” or “3:30:42pm PST”
} NSDateFormatterStyle;
举例:
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];
NSDateFormatter相关整理,布布扣,bubuko.com
时间: 2024-10-14 19:43:44