Xcode
iOS
Swift
swift4

Swift で現在の年月日時分秒を取得する

// 現在日時
var date = Date()

// 年月日時分秒をそれぞれ個別に取得
var calendar = Calendar.current
var year     = calendar.component(.year,   from: date)
var month    = calendar.component(.month,  from: date)
var day      = calendar.component(.day,    from: date)
var hour     = calendar.component(.hour,   from: date)
var minute   = calendar.component(.minute, from: date)
var second   = calendar.component(.second, from: date)