HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在iOS中将日,月和年确定为int大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想确定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];

大佬总结

以上是大佬教程为你收集整理的如何在iOS中将日,月和年确定为int全部内容,希望文章能够帮你解决如何在iOS中将日,月和年确定为int所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。