How to combine date and time from two UIDatePickers?

前端 未结 3 1521
死守一世寂寞
死守一世寂寞 2021-01-04 02:24

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.

3条回答
  •  [愿得一人]
    2021-01-04 03:23

    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);
    

提交回复
热议问题