-2
我知道這個問題已被問了這麼多時間,可能是一些問題的重複,實際上我試圖通過在String中轉換它們將日期存儲到數組中。我需要在NSDate格式的值,所以我再次將存儲的字符串轉換爲日期。問題與格式日期
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:@"MM-dd-yy-hh-mm-ss"];
NSString *date = [dateformat stringFromDate:datePicker.date];
[kAppDelegate.glbArrName addObject:date];
但我得到這樣的輸出:
NSString *date = [kAppDelegate.glbArrDate objectAtIndex:indexPath.row];
NSLog(@"Date of birth %@",date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"MM-dd-yy-hh-mm-ss"];
NSDate *birthDate = [[NSDate alloc] init];
birthDate = [dateFormatter dateFromString:date];
NSLog(@"Date of birth after formatting %@",birthDate);
輸出是:
Date of birth 04-05-16-06-38-14
Date of birth after formatting 2016-04-05 01:08:14 +0000
爲什麼它的變化形式,因爲我已經做了與以前相同。請幫助我找出答案。
爲什麼麻煩轉換字符串和從字符串轉換?一個數組可以容納一個'NSDate'而沒有任何問題。 – Avi
其實我也需要格式化,如何直接從uipicker.date獲得NSdate,也格式化..我實際上得到字符串 –
在你的數組中存儲'NSDate'。除非需要將其顯示給用戶,否則不要將其轉換爲字符串。 – rmaddy