Skip to content

Instantly share code, notes, and snippets.

@justinmfischer
Last active October 6, 2021 13:22
Show Gist options
  • Save justinmfischer/0a6edf711569854c2537 to your computer and use it in GitHub Desktop.
Save justinmfischer/0a6edf711569854c2537 to your computer and use it in GitHub Desktop.
Combine date and time into a single NSDate in Swift 2.0
func combineDateWithTime(date: NSDate, time: NSDate) -> NSDate? {
let calendar = NSCalendar.currentCalendar()
let dateComponents = calendar.components([.Year, .Month, .Day], fromDate: date)
let timeComponents = calendar.components([.Hour, .Minute, .Second], fromDate: time)
let mergedComponments = NSDateComponents()
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.dateFromComponents(mergedComponments)
}
@donniefitz2
Copy link

Thanks for posting this. It was a big help.

@mikemilla
Copy link

Thank you

@aetsyss
Copy link

aetsyss commented Nov 11, 2016

Swift 3 version

func combineDateWithTime(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)
}

@abesmon
Copy link

abesmon commented Sep 12, 2017

Thanks. Save a lot of my time :) 👍

@Sharkesm
Copy link

Sharkesm commented Oct 8, 2017

Searched for this piece of code for days. Thanks m8

@majeedyaseen
Copy link

Thankyou Saved my Day. :) 👍

@awjenson
Copy link

Thanks! How should we create the properties for date and time?

@antonyraphel
Copy link

Worked!!! 👍 💯

@Theo58
Copy link

Theo58 commented Apr 23, 2019

After multiple year it still is a very nice solution. Thanks

@sgbasaraner
Copy link

Swift 5 version, typo fixed and without force unwrapping:

func combineDateWithTime(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 mergedComponents = DateComponents()
        mergedComponents.year = dateComponents.year
        mergedComponents.month = dateComponents.month
        mergedComponents.day = dateComponents.day
        mergedComponents.hour = timeComponents.hour
        mergedComponents.minute = timeComponents.minute
        mergedComponents.second = timeComponents.second
        
        return calendar.date(from: mergedComponents)
    }

@JorgeZapata27
Copy link

Genius! Thanks so much, you saved so much time for all of us!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment