In Swift 2:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
Swift 3?
let date = Date()
let calendar = Calendar. current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar. component(.second, from: date)
print("hours = \(hour):\(minutes):\(seconds)")
You can get these in the corresponding way Era, year, month, date, etc.
How to determine the hour, minute and second of the NSDate class in Swift 3?
In Swift 2:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.Hour, fromDate: date)
let hour = components.hour
Swift 3?
In Swift 3.0, Apple removed the “NS” prefix and made everything simple. The following is to get the hours, minutes and seconds from the’Date’ class的方法(NSDate alternate)
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
print("hours = \(hour):\(minutes):\(seconds)")
Like these, you can get the era, year, month, date, etc. in a corresponding way.