HOW TO PICK DATE AND TIME SEPARATELY IN IOS?

Sanjay Sharma

Total Post:45

Points:315
Posted by  Sanjay Sharma
 530  View(s)
Ratings:
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..!

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How to pick Date and Time separately in iOS?

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!