2012-10-30 13 views
15

Mam UIDatePicker i otrzymuję wybraną datę z tego w formacie yyyy-MM-dd. Teraz chcę przekonwertować go na format MM/dd/yyyy .. Jak mogę to zrobić?Konwertuj datę w formacie MM/dd/rrrr w kodzie X?

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

Kupuję 2012-10-30, ale chcę 10/30/2012.

+0

To nie jest naprawdę do czynienia z Xcode, to zrobić z ram Cocoa Touch. –

+0

http://unicode.org/reports/tr35/tr35-10.html Dodatek F jest twoim przyjacielem – jackslash

+0

Uh, zmień format formatowania daty? –

Odpowiedz

46

patrz instrukcja lub wprowadzenie z każdej linii

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

Dlaczego warto przekonwertować datę na ciąg do daty na ciąg ??? –

+1

@HotLicks hej mate Po prostu zamieszczam tę odpowiedź z instrukcją, więc każdy łatwo zrozumie cały przepływ, więc po prostu wkładam ten FLOW ... :) –

+1

@HotLicks Ponieważ oryginalne pytanie pochodzi od rrrr-MM-dd. Ta odpowiedź obejmuje to. Więc proszę, jeśli zagłosujesz. Wyjaśnij dlaczego. – doge

1

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

+0

Zrobiłem to. Nie mogłem go zdobyć. – Honey

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

Wystarczy zobaczyć, jak kończy się:

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

Oryginalny odpowiedź Zaakceptowany robi kilka rzeczy dodatkowe:

1) Przydziela dwie instancje NSDateFormatter, które nie są wymagane. NSDateFormatter jest drogi do stworzenia.

2) Lepiej jest wydać formaty daty jawnie, gdy skończysz, zamiast odroczonego wydania za pomocą 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; 
} 

Ciesz

Powiązane problemy