Created
March 16, 2018 18:01
-
-
Save majeedyaseen/ec0cefddce4a21cf69d87ec8bc5d8895 to your computer and use it in GitHub Desktop.
Combine date and time into a single NSDate in Swift 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//function to combine date and time from two different date objects. | |
func combineDateandTime(date: Date, time: Date) -> Date? { | |
let calendar = NSCalendar.current | |
let dateComponents = calendar.dateComponents([.year, .month, .day], from: date) | |
let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time) | |
var mergedComponments = DateComponents() | |
mergedComponments.year = dateComponents.year! | |
mergedComponments.month = dateComponents.month! | |
mergedComponments.day = dateComponents.day! | |
mergedComponments.hour = timeComponents.hour! | |
mergedComponments.minute = timeComponents.minute! | |
mergedComponments.second = timeComponents.second! | |
return calendar.date(from: mergedComponments) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment