0

I get time from UIDatePicker and another date (NSDate) from particular date (name is myDate).
I want to add (only) time of UIDatePicker to (at place of myDate'S time) myDate.

My goal is fire NSLocalNotification at proper time:

I follow This Question but it dose not solve my issue.

How can i do this?

Community
  • 1
  • 1

3 Answers3

1

The question you link to is a good guide. You should get the date components for the time from the date picker (this is the amount of time in hours, minutes and seconds that you want to add) and then use the calendar to add those date components to your current date.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

try this hopes helps u

 NSCalendar *currentCalender = [NSCalendar autoupdatingCurrentCalendar];
 NSDate *timeDate = [self.datePicker date];
 NSDateComponents *timeComponents = [currentCalender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:timeDate]


 NSDateComponents *dateComponents = [currentCalender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: myDate];


 NSDateComponents *setDate = [[NSDateComponents alloc] init];
[setDate setDay:dateComponents.day];
[setDate setMonth:dateComponents.month];
[setDate setYear:dateComponents.year];

[setDate setHour:timeComponents.hour];
[setDate setMinute:timeComponents.minute];
[setDate setSecond:timeComponents.second];

NSDate *date = [currentCalender dateFromComponents:setDate];


Shankar BS
  • 8,394
  • 6
  • 41
  • 53
0

Hi Modify This Code Hope you will get your solution.

NSDate *date = d;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];   // if you want to show the date to the user
[df setTimeStyle:NSDateFormatterShortStyle];   // if you want to show the date to the user
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];  // if you want to use the date for your model
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone:destinationTimeZone];

NSString *dateString = [df stringFromDate:date];
NSLog(@"%@", dateString);

Let me know still if you have any query?

kb920
  • 3,039
  • 2
  • 33
  • 44