Skip to content

Instantly share code, notes, and snippets.

View justinmfischer's full-sized avatar
🎯
Focusing

Justin Fischer justinmfischer

🎯
Focusing
View GitHub Profile
@justinmfischer
justinmfischer / combineDateWithTime
Last active October 6, 2021 13:22
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
@justinmfischer
justinmfischer / roundDateTo5Minutes
Last active December 26, 2015 22:09
Round Date To 5 Minutes
+ (NSDate *) roundDateTo5Minutes:(NSDate *) mydate {
NSDateComponents *time = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:mydate];
NSInteger minutes = [time minute];
int remain = minutes % 5;
if (remain < 3){
mydate = [mydate dateByAddingTimeInterval:-60 * (remain)];
}else{
mydate = [mydate dateByAddingTimeInterval:60 * (5-remain)];