iPhone - 指定された日付から整数として年を返すためにどのように?

StackOverflow https://stackoverflow.com/questions/1544988

  •  20-09-2019
  •  | 
  •  

質問

私は整数として任意の日から4桁の年を返すようにしようとしています。私はNSCalendarやNSDate機能を備えた強力ではないんだけど、無い成功した日付と日付フォーマッタからの部品でそれらを使用して試してみました。

感謝します。

役に立ちましたか?

解決

NSCalendarから<のhref = "http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/Reference/NSCalendar.html#//apple_ref/occ/instm/ NSCalendar /コンポーネント:fromDateから」REL = "nofollowをnoreferrer">文書はを

unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];

そして、あなたが実行して年を取得することができます:

NSInteger year = [comps year];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top