16

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];
Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
Midas
  • 930
  • 2
  • 8
  • 20
  • There are so many result for this http://www.google.co.in/search?q=objective+c+complare+date+and+time&aq=f&oq=objective+c+complare+date+and+time&sourceid=chrome&ie=UTF-8#hl=en&tbo=d&spell=1&q=objective+c+compare+date+and+time&sa=X&ei=BrUIUaruHInprQeL-4DwDg&ved=0CC4QvwUoAA&bav=on.2,or.r_gc.r_pw.r_qf.&bvm=bv.41642243,d.bmk&fp=262c79f7a17c7860&biw=1279&bih=576 what is your problem for which u r not getting soluction there? – CRDave Jan 30 '13 at 05:54
  • @CRDave: I tried so many methods. But I repeatedly getting the combined date as 2001-01-01 or so. and the notification comes at the time is saved. – Midas Jan 30 '13 at 06:12
  • Put some line of current code like in which format u get date n time. as NSString or as NSDate and how u r combining. I hope it will help someone to help u back. – CRDave Jan 30 '13 at 06:15
  • @CRDave i think you read it incorrectly, its not comparing,its combining date and time.. – Midas Jan 30 '13 at 06:16
  • Have you connected your IBOutlets up, because I copied your code and I get the correct date and time in dateComponents (after you do setHour and setMinute). – rdelmar Jan 30 '13 at 06:48
  • @rdelmar: i have rechecked my iboutlets. they are ok.. – Midas Jan 30 '13 at 07:16
  • Try logging date1 and date2 to see what they return (I got 2001-01-01 for the date when my date picker wasn't hooked up properly). – rdelmar Jan 30 '13 at 07:58
  • @rdelmar: i am also getting the same date.. and i dont have iboutlet for the datepicker because i created it programmatically.. – Midas Jan 30 '13 at 08:22
  • need more explanation... – Rajneesh071 Jan 30 '13 at 10:28
  • Explanation on which part? – Midas Jan 30 '13 at 10:29

3 Answers3

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);
Felix
  • 35,354
  • 13
  • 96
  • 143
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • DEPRECATED - 8.0 NSCalendarUnitYear, NSCalendarUnitMonth, NSCalendarUnitDay NSCalendarUnitHour, NSCalendarUnitMinute, NSCalendarUnitSecond – Alex Hedley Aug 29 '17 at 12:55
5

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)
}
Ahmed M. Hassan
  • 709
  • 9
  • 14
2

Try this:

Convert date into NSString and Time into NSString.

Apppend Time into date string.

Now convert that final string into NSDate

Example: (Add validations from ur side)

 NSDate *date1 = [datePicker date];
    NSDate *date2 = [timePicker date];

    NSString *date = [NSString stringWithFormat:@"%@",date1];
    NSString *time = [NSString stringWithFormat:@"%@",date2];

    NSString *dateAndTime = [NSString stringWithFormat:@"%@ %@",date,time];
    NSDate *dateTime = [self dateFromString:dateAndTime];



- (NSDate*) dateFromString:(NSString*)aStr
{


    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
    //[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss a"];
    [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    NSLog(@"%@", aStr);
    NSDate   *aDate = [dateFormatter dateFromString:aStr];
    [dateFormatter release];
    return aDate;
}
Satish Azad
  • 2,302
  • 1
  • 16
  • 35