如何从NSDate获得月份和年份?

时间:2022-08-25 10:51:59

Is there any way/ method to seperate month & year form NSDate?

是否有任何方式/方法来分隔NSDate的月份和年份?

I just need to display current month & year? Any Example?

我只需要显示当前的月份和年份?任何例子?

2 个解决方案

#1


1  

This could be done by using NSDateComponents

这可以通过使用NSDateComponents来完成

#2


6  

NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
[calendar release];

cheers endo

#1


1  

This could be done by using NSDateComponents

这可以通过使用NSDateComponents来完成

#2


6  

NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
[calendar release];

cheers endo