Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Make a formatter like this</p> <pre><code>NSDateFormatter* format = [[NSDateFormatter alloc]init]; [format setDateFormat:@"YY/MM/dd"]; </code></pre> <p>Get the formatted string from a an NSDate like this</p> <pre><code>NSString* testDate = [format stringFromDate:[NSDate date]]; </code></pre> <p>Get an NSDate back from a string like so.</p> <pre><code>NSDate* reformDate = [format dateFromString:testDate]; </code></pre> <p>Release if not in ARC, otherwise set to nil [format release]; or format = nil;</p> <p>You could modify your function to look something like this. This would set the formatted date string as your UITextField's text.</p> <pre><code>- (void)updateTextField:(id)sender { UIDatePicker *picker = (UIDatePicker*)self.celebration.inputView; NSDateFormatter* format = [[NSDateFormatter alloc]init]; [format setDateFormat:@"YY/MM/dd"]; NSString* formattedDate = [format stringFromDate:picker.date]; self.celebration.text = formattedDate; format = nil; } </code></pre> <p>That being said The UIDatePicker itself doesn't support formatting like this. You can reference the class here. <a href="http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html" rel="nofollow">UIDatePicker Class Reference</a></p> <p>If you are really set on this format for a datePicker you would probably have to build a custom one. However, I would discourage this since the built in UIDatepicker takes a user's Locale and language into consideration automatically while your format would lock it into your preferred format (which isn't always correct for your user depending on where they are in the world).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload