Combine Date and Time into a Single Date iOS Swift 3
func resetTime(forDate:NSDate,timedate:NSDate) ->Date {
let df = DateFormatter()
df.dateFormat = "dd MMM yyyy"
var resultdate = Date()
if let dateFromString = df.date(from: df.string(from: forDate as Date)) {
let hour = NSCalendar.current.component(.hour, from: timedate as Date)
let minutes = NSCalendar.current.component(.minute, from: timedate as Date)
if let dateFromStringWithTime = NSCalendar.current.date(bySettingHour: hour, minute: minutes, second: 0, of: dateFromString) {
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS'Z"
let resultString = df.string(from: dateFromStringWithTime)
resultdate = df.date(from: resultString)!
}
}
return resultdate
}
Comments
Post a Comment