我想确定iOS中当前日期的日,月和年.我在网上搜索后发现了这个:
NSDate *date = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date]; NSInteger Day = [components day]; NSInteger month = [components month]; NSInteger year = [components year];
此代码显示当天正确,但年份和月份未显示应该如此.它显示了一些数字,如2147483647.我找不到解决方案,所以请帮我纠正我找到的代码,或者写一些新的代码来完成我需要的工作.
解决方法
这一行是问题所在:
NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date];
它应该是
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。