2012-10-30 52 views
15

我有一個UIDatePicker,我從yyyy-MM-dd格式中得到它的選定日期。現在我想將它轉換爲MM/dd/yyyy格式..我該怎麼做?在xcode中以MM/dd/yyyy格式轉換日期?

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

我得到2012-10-30但我想要10/30/2012。

+0

這是不是真的在Xcode做的,它是用可可觸摸框架做。 – 2012-10-30 12:31:14

+0

http://unicode.org/reports/tr35/tr35-10.html附錄F是你的朋友 – jackslash

+0

呃,改變日期格式化格式? –

回答

46

參閱說明書或介紹每行

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

爲什麼要從日期轉換爲字符串日期到字符串??? –

+1

@HotLicks嘿夥計我只是發佈這個答案的指示,所以任何一個很容易理解整個流程,所以我只是把這個FLOW ... :) –

+1

@HotLicks因爲原來的問題起源於yyyy-MM-dd。這個答案涵蓋了。所以請,如果你投下了票。解釋爲什麼。 – doge

1

df.dateFormat = @「MM/dd/yyyy」;

+0

我做了它,但無法得到它 – Honey

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

只是爲了看看它是如何結束:

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

原來接受的答案做了幾件事情額外:

1)它分配兩個不需要的NSDateFormatter實例。 NSDateFormatter創建起來很昂貴。

2)最好是在你完成它們時明確地發佈日期格式化程序,而不是使用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; 
} 

享受