16

I have a UIDatePicker and I m getting the selected date from it in yyyy-MM-dd format.Now I want to convert it to MM/dd/yyyy format ..How can i do it ?

NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"yyyy-MM-dd";
NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];

[dateString2 release];
dateString2=nil;
dateString2= [[NSString alloc]initWithString:[temp objectAtIndex:0]];
NSLog(@"%@",dateString2);

Im getting 2012-10-30 but I want 10/30/2012.

Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
Honey
  • 2,840
  • 11
  • 37
  • 71

9 Answers9

45

See Instruction or introduction with every line

NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(@"Converted String : %@",convertedString);
Mrunal
  • 13,982
  • 6
  • 52
  • 96
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • 1
    @HotLicks hey mate i just post this answer with instruction so any one easily understand whole flow so i just put this FLOW... :) – Paras Joshi Oct 30 '12 at 13:10
  • 1
    @HotLicks Because the original question originated from a yyyy-MM-dd. This answer covers that. So please, if you down voted. Explain why. – Jens Bergvall Oct 30 '12 at 14:48
  • @Da_smokes -- The original question originated from an NSDate coming from a UIDatePicker. – Hot Licks Oct 30 '12 at 16:00
1

df.dateFormat = @"MM/dd/yyyy";

Eric
  • 4,063
  • 2
  • 27
  • 49
1
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
1

Just to see how it ends up:

NSDateFormatter* df = [[[NSDateFormatter alloc]init] autorelease];
df.dateFormat = @"MM/dd/yyyy";
NSString* dateString = [df stringFromDate:datePicker.date];  // Don't use leading caps on variables
NSLog(@"%@", dateString);
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
1
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    
[formatter setDateFormat:@"MM/dd/yyyy"];    
1

The original accepted answer does a few things extra:

1) It allocates two NSDateFormatter instances which is not required. NSDateFormatter is expensive to create.

2) It is better to release date formatters explicitly when you are done with them rather than a deferred release using autorelease.

//get datepicker date (NSDate *)
NSDate *datePickerDate = [myDatePicker date];

//create a date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 

//set its format as the required output format
[formatter setDateFormat:@"MM/dd/yyyy"];

//get the output date as string
NSString *outputDateString = [formatter stringFromDate:datePickerDate];

//release formatter
[formatter release];
SayeedHussain
  • 1,696
  • 16
  • 23
1
- (void)showTime {
   NSDate *now = [NSDate date];
   NSDateFormatter *dateFormatter = [NSDateFormatter new];
   [dateFormatter setDateFormat:@"HH:mm:ss"];
   // you label
   _time.text = [dateFormatter stringFromDate:now]; 
}

- (void)showDate {
   NSDate *now = [NSDate date];
   NSDateFormatter *dateFormatter = [NSDateFormatter new];
   [dateFormatter setDateFormat:@"MM/dd/yyyy"];
   // you label
   _date.text = [dateFormatter stringFromDate:now];
}

- (void)viewDidLoad {
   [super viewDidLoad];
    self.showTime;
    self.showDate;
}

Enjoy

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
Erinson
  • 78
  • 8
0
NSString *str = @"2012-10-30"; /// here this is your date with format yyyy-MM-dd

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; here convert date in NSString
NSLog(@"Converted String : %@",convertedString);
Borbea
  • 412
  • 6
  • 12
mahesh chowdary
  • 432
  • 5
  • 13
0

I use a code with options NSISO8601DateFormatWithDashSeparatorInDate:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString * stringformat = [NSDateFormatter dateFormatFromTemplate:@"MMddy" options:NSISO8601DateFormatWithDashSeparatorInDate locale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:stringformat];

NSDate *d = [NSDate date]; // or set your date
NSLog(@"date in format %@",  [dateFormatter stringFromDate:d]); // date in format 2018-06-20
Agisight
  • 1,778
  • 1
  • 14
  • 15