如何在iOS中将日,月和年确定为int

如何在iOS中将日,月和年确定为int,第1张

概述我想确定iOS中当前日期的日,月和年.我在网上搜索后发现了这个: NSDate *date = [NSDate date];NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDateComponents *components = [calendar comp 我想确定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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1097536.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存