int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate *date=[NSDate date];
NSLog(@"%@",date);
//借助辅助类简单的格式化
NSCalendar *cal=[NSCalendar currentCalendar];
NSDateComponents *coms= [cal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
NSLog(@"year:%ld,month:%ld,day:%ld",coms.year,coms.month,coms.day);
//不借助辅助类 格式化
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
formatter.dateFormat=@"yyyy年MM月dd日 HH:mm:ss";
//formatter.dateFormat=@"yyyy年MM月dd日 hh:mm:ss";
NSString *datestring=[formatter stringFromDate:date];
NSLog(@"%@",datestring);
//演示日期的加减法
NSDateFormatter *dForma=[[NSDateFormatter alloc]init];
dForma.dateFormat=@"yyyy年MM月dd日 hh:mm:ss";
//设置时间间隔
NSTimeInterval t=24*60*60;
//t:时间间隔可以设置为正负,正表示今天之后,负表示,今天之前
NSDate *tomorrow=[NSDate dateWithTimeIntervalSinceNow:t];
NSString *tomStr=[dForma stringFromDate:tomorrow];
NSLog(@"%@",tomStr);
}
return 0;
}