2

In my app I am using date picker & show date in textfield,

Date picker given me a date in this format "Mar 1, 2012"

I want that date in "dd/MM/yyyy" format.

how can i do that?

Cyrille
  • 25,014
  • 12
  • 67
  • 90
Anki
  • 589
  • 2
  • 13
  • 22

2 Answers2

8

The date picker displays "Mar 1, 2012" but internally it stores the date as a NSDate, which you can convert to a NSString by using a NSDateFormatter, specifying the format you want:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *formattedDate = [df stringFromDate:yourDatePicker.date];
[df release];

The reference for the format string is here: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • You're welcome. As commented in your question, though, you could try a bit of googling before asking. General rule here: you're welcome to ask questions, but describe what you've tried, what doesn't work, and what you're expecting. Welcome to SO! – Cyrille Mar 01 '12 at 07:46
  • i tried with NSDateFormatter but my in my problem , i m not passing the DatePicker.date,get this problem . by the way Thanks, once again for solving this problem . – Anki Mar 01 '12 at 07:51
  • Hey Cyrille, How you set NSDatepicker display date format to "Mar 1, 2012". can you please describe me? In my case is display in "dd/MM/yyyy" format, but I want 'MM/dd/yyyy' date format to display date in NSDatePicker – Punita Sep 30 '15 at 12:31
  • You have no control over this. It's the language and locale defined by the user in their device settings that determines the presentation of date pickers through the whole OS. – Cyrille Oct 02 '15 at 07:28
4

try the following code:

 NSDate *today = [NSDate date];
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"dd/MM/yyyy hh:mma"];
 NSString *dateString = [dateFormat stringFromDate:today];
 NSLog(@"date: %@", dateString);
 [dateFormat release];
Vikram
  • 8,235
  • 33
  • 47