どうも、ちょげ(@chogetarou)です。
Dateの年を取得する方法を紹介します。
方法

Dateの年を取得するには、Calendarを使います。
まず、Caledar.current.component()を呼び出します。
そして、componentの第1引数に「.year」、第2引数「from」に対象のDateを指定します。
let year = Calendar.current.component(.year, from: date)
Calendar.current.componetによって、対象のDateの年を取得することが出来ます。
使用例
let today = Date()
let year = Calendar.current.component(.year, from: today)
print(today)
print(year)
オススメの記事
コメント