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

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