Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine date and time from two UIDatePickers?

I have two UIDatePickers in my app, one mode is for selecting date and other to time mode. I have fired a notification when the exact date and time is reached. I'm able to fire the reminder at correct time, but the problem is that the date is not checked. Is there any way to check the date and time at the same time??

Thanx in advance...

Edit

NSDate *date1 = [datePicker date];
NSDate *date2 = [timePicker date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlagsDate fromDate:date1];
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit ;
NSDateComponents *timeComponents = [gregorian components:unitFlagsTime fromDate:date2];

[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];

NSDate *combDate = [gregorian dateFromComponents:dateComponents];   

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = combDate;
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Alert!";
localNotif.soundName = UILocalNotificationDefaultSoundName;  
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
like image 538
Midas Avatar asked Jan 30 '13 05:01

Midas


2 Answers

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:self.datePicker.date];
NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:self.timePicker.date];

NSDateComponents *newComponents = [[NSDateComponents alloc]init];
newComponents.timeZone = [NSTimeZone systemTimeZone];
[newComponents setDay:[dateComponents day]];
[newComponents setMonth:[dateComponents month]];
[newComponents setYear:[dateComponents year]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];

NSDate *combDate = [calendar dateFromComponents:newComponents]; 

NSLog(@" \ndate : %@ \ntime : %@\ncomDate : %@",self.datePicker.date,self.timePicker.date,combDate);
like image 182
Anil Varghese Avatar answered Oct 08 '22 12:10

Anil Varghese


Swift 5 version of the accepted answer if it might help.

/// Returns `Date` from date and time.
func combine(date: Date, time: Date) -> Date? {
    let calendar = Calendar.current
    let dateComponents = calendar.dateComponents([.day, .month, .year], from: date)
    let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)
    
    var newComponents = DateComponents()
    newComponents.timeZone = .current
    newComponents.day = dateComponents.day
    newComponents.month = dateComponents.month
    newComponents.year = dateComponents.year
    newComponents.hour = timeComponents.hour
    newComponents.minute = timeComponents.minute
    newComponents.second = timeComponents.second
    
    return calendar.date(from: newComponents)
}
like image 20
Ahmed M. Hassan Avatar answered Oct 08 '22 12:10

Ahmed M. Hassan