Home > DeveloperSection > Forums > How to pick Date and Time separately in iOS?
Sanjay Sharma
Sanjay Sharma

Total Post:45

Posted on    March-28-2016 1:25 AM

 iPhone iPhone  Ios  Objective C  Ipad 

 1 Reply(s)
 245  View(s)
Rate this:

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..!

Tarun Kumar

Total Post:210

Posted on    March-29-2016 12:01 AM

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.

Don't want to miss updates? Please click the below button!

Follow MindStick