如何以整数格式获取NSDate日,月,年? [英] How to get NSDate day, month and year in integer format?
本文介绍了如何以整数格式获取NSDate日,月,年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以整数形式获取 NSDate
的日期,月份和年份组成部分,即如果日期是1/2/1988,那么我应该获得1,2和1988分别作为整数。如何在iOS中执行此操作?我发现类似的问题,但方法 descriptionWithCalendarFormat
:发出警告,似乎现在已经弃用。
I want to get the day, month and year components of NSDate
in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer. How can I do this in iOS? I found the similar question but the method descriptionWithCalendarFormat
: gives a warning and seems to be deprecated by now.
推荐答案
这里是
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate]; // Get necessary date components
[components month]; //gives you month
[components day]; //gives you day
[components year]; // gives you year
你可以像上面那样使用NSDateComponents。
You can use NSDateComponents for that as above.
请访问这个页面的详细信息。
希望它有帮助。
这篇关于如何以整数格式获取NSDate日,月,年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文