This might be handy for those who want to use the current date in more than one class.
extension String {
func getCurrentTime() -> String {
let date = Date()
let calendar = Calendar.current
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
let realTime = "\(year)-\(month)-\(day)-\(hour)-\(minutes)-\(seconds)"
return realTime
}
}
Usage
var time = ""
time = time.getCurrentTime()
print(time) // 1900-12-09-12-59