Combine Date and Time into a Single Date iOS Swift 3



I had written a common method for combined date and time because if you set one date with only date and another one with Time only then if you want to combine both into a single date then follow the code
Code:
    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

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift