2016-04-05 145 views
-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 

爲什麼它的變化形式,因爲我已經做了與以前相同。請幫助我找出答案。

+0

爲什麼麻煩轉換字符串和從字符串轉換?一個數組可以容納一個'NSDate'而沒有任何問題。 – Avi

+0

其實我也需要格式化,如何直接從uipicker.date獲得NSdate,也格式化..我實際上得到字符串 –

+0

在你的數組中存儲'NSDate'。除非需要將其顯示給用戶,否則不要將其轉換爲字符串。 – rmaddy

回答

0

您正在將日期字符串重寫回NSDate,然後打印出NSDate,默認爲後者2016-04-05 01:08:14 +0000格式,作爲NSDate的-description的一部分。

+0

我明白了你的觀點,但是如何得到確切的日期 –