2012-10-30 10 views
15

Ho un UIDatePicker e sto ottenendo la data selezionata da esso nel formato yyyy-MM-dd.Ora voglio convertirlo nel formato MM/dd/yyyy .. Come posso farlo?Convertire la data nel formato gg/mm/aaaa in xcode?

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 ottenere 2012-10-30 ma voglio 30/10/2012.

+0

Questo non è davvero a che fare con Xcode, è a che fare con il framework Cocoa Touch. –

+0

http://unicode.org/reports/tr35/tr35-10.html Appendice F è il tuo amico – jackslash

+0

Uh, cambia il formato di formattazione della data? –

risposta

46

Vedere istruzioni o l'introduzione di ogni riga

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); 
+0

Perché convertire dalla data alla stringa fino alla data ?? –

+1

@HotLicks hey mate ho appena postato questa risposta con le istruzioni in modo che chiunque possa capire facilmente tutto il flusso quindi ho appena messo questo FLUS ... :) –

+1

@HotLicks Perché la domanda originale ha avuto origine da aaaa-MM-gg. Questa risposta copre questo. Quindi, per favore, se hai votato. Spiega perchè. – doge

1

df.dateFormat = @ "MM/gg/aaaa";

+0

L'ho fatto.ma non potevo ottenerlo – Honey

1
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
[formatter setDateFormat:@"MM/dd/yyyy"]; 
1

solo per vedere come va a finire:

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); 
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); 
1
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
[formatter setDateFormat:@"MM/dd/yyyy"];  
1

la risposta accettata originale non poche cose extra:

1) Alloca due istanze NSDateFormatter che non è richiesta. NSDateFormatter è costoso da creare.

2) È preferibile rilasciare i formattatori di data in modo esplicito quando si ha finito con loro piuttosto che un rilascio posticipato mediante 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]; 
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; 
} 

Godetevi