1

I have two date first get from DatePicker (self.datePicker.date) and another is manually set NSDate (self.fireDate).

I want to add time of self.datePicker.date to self.fireDate.

My Logic is: I Follow this Question

NSCalendar *currentCalender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *timeComponents = [currentCalender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[self.datePicker date]];
    NSDateComponents *dateComponents = [currentCalender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: self.fireDate];
    [dateComponents setHour:timeComponents.hour];
    [dateComponents setMinute:timeComponents.minute];
    [dateComponents setSecond:timeComponents.second];
    NSDate *finalDate = [currentCalender dateFromComponents:dateComponents];

NSLog(@"%@", [self.datePicker date]);
NSLog(@"%@", finalDate);
NSLog(@"%@", self.fireDate);

Please give me any suggestion on this issue.

Thanks in advance ;)

Community
  • 1
  • 1
  • 2
    If `self.fireDate` and `[self.datePicker date]` already have the same value, then the problem is not in this code (which looks correct at first sight). – Martin R Oct 16 '13 at 07:03
  • @MartinR - sorry.. i understand it but i checked .. it is not same value :( –  Oct 16 '13 at 07:08
  • As @MartinR suggests - check the values before this calculation, and maybe give the output here as well. – Eiko Oct 16 '13 at 07:09
  • 1
    @RanjuPatel: Then your statement *"i got same date (finalDate, self.fireDate and [self.datePicker date])"* is misleading. Perhaps you should show the output of all the NSLogs. – Martin R Oct 16 '13 at 07:11
  • @RanjuPatel What's the actual problem, now? – Nikolai Ruhe Oct 16 '13 at 07:19
  • 1
    @RanjuPatel: It is difficult to help if you don't provide more details, e.g. the NSLog outputs. – Martin R Oct 16 '13 at 11:01

1 Answers1

4

The calculations NSCalendar does are subject to the locale and time zone of the calendar. To get the proper time components you have to use the calendar of the picker:

NSDateComponents *timeComponents = [self.datePicker.calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[self.datePicker date]];

The dateComponents and finalDate should probably be calculated from the current calendar as set by the user in Settings.app which you get from ...

NSCalendar *currentCalender = [NSCalendar currentCalendar];

Another Wild Guess: Indentation of the posted code suggests that you did not paste all of your real code. Maybe you set fireDate in an enclosing scope but print another one from the outer scope?

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • fireDate is Date that set by user for alarm nothing more.. it is just Date such like ... `2013-10-16 07:03:57 +0000`.. and also i put my whole related code :) Thanks Sir :) –  Oct 16 '13 at 07:04