How to pick Date and Time separately in iOS?

I am using NSDatePickerView controller in our iPhone app. I have a need to set the Date and Time in different position. So, I use NSDatePickerModeDate to pick Date and NSDatePickerModeTime to pick Time.

But both of them are referring to same NSDate object inside a NSMutableDictionary, and I know about the NSDatePickerModeDateTime, but our need is to get the Date & Time spearatedly.
Please help me..!

Last updated:3/29/2016 12:01:04 AM

1 Answers

Tarun Kumar
Tarun Kumar

If you want to get separate Date and Time then we need to convert full date to only date format and convert date to only time format. below we are provided both examples:
// Convert to Date Format

- (IBAction) convertToDate:(UIDatePickerView *)sender {
 NSDateFormatter *date_format = [[NSDateFormatter alloc] init];
 [date_format setDateFormat:@"MM/DD/YYYY"];
 NSString *date = [date_format stringFromDate:[sender date]];
 [data setObject:date forKey:@"date"];

// Convert to Time format

- (IBAction) convertToTime:(UIDatePickerView *)sender {
 NSDateFormatter *time_format = [[NSDateFormatter alloc] init];
 [time_format setDateFormat:@"HH:MM a"];
 NSString *time = [time_format stringFromDate:[sender date]];
 [data setObject:time forKey:@"Time"];

I hope above example is helpful for you.