iOS时间以yyyy-MM-dd格式输出的两种方法代码
//方法一
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
// [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString * locationString=[formatter stringFromDate:[NSDate date]];
NSLog(@"今天日期是:%@",locationString);
//方法二:
NSCalendar *calendar = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now;
NSDateComponents *comps = [[NSDateComponentsalloc] init];
NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
now=[NSDate date];
comps = [calendar components:unitFlags fromDate:now];
NSLog(@"comps=%@",comps);
int year=[comps year];
int week = [comps weekday];
int month = [comps month];
int day = [comps day];
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
NSLog(@"year=%d,week=%d,month=%d,day=%d,hour=%d,min=%d,sec=%d",year,week,month,day,hour,min,sec);
NSLog(@"今天日期是:%d-%d-%d",year,month,day);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。