2013-05-01 44 views
0

我有以下的代碼被稱爲的cellForRowAtIndexPath爲一個UITableView。NSDateFormatter不格式化字符串

self.dateFormatter = [[NSDateFormatter alloc] init]; 
[self.dateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"]; 

OLVisit *visit = [self.visits objectAtIndex:indexPath.row]; 
NSString *strVisitDate = [NSString stringWithFormat:@"%@", visit.olcreatedat]; 
NSDate *visitDate = [self.dateFormatter dateFromString:strVisitDate]; 
strVisitDate = [self.dateFormatter stringFromDate:visitDate]; 

的字符串從visit.olcreatedat返回是按以下格式「2013年4月9日11點55分25秒0000」(如可以在下面的調試器屏幕截圖所示)。

enter image description here

我得到一個零NSDate的值返回到visitDate。

可能有人幫我想出解決辦法?

+2

也許如果你設置日期格式以匹配日期的格式,你會有更好的運氣。 – 2013-05-01 20:32:01

+1

[如何解析日期字符串到iOS中的NSDate對象?](http://stackoverflow.com/questions/4999396/how-to-parse-a-date-string-into-an-nsdate- object-in-ios) – 2013-05-01 20:33:30

+0

'visit.olcreatedat'類型是什麼?它是一個'NSString'嗎?如果是這樣,請刪除對'stringWithFormat:'的調用。 – rmaddy 2013-05-01 20:41:15

回答

2

UPDATE:正如@rmaddy指出的那樣,對於visit.olcreatedat是什麼有一個誤解。 這第一個答案是基於這樣的假設visit.olcreatedat是 的NSDate。對於假定visit.olcreatedat是包含格式化日期的NSString的答案,請閱讀本答案的第二部分。

visit.olcreatedat是一個NSDate

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Alloc the date formatter in a method that gets run only when the controller appears, and not during scroll operations or UI updates. 
    self.outputDateFormatter = [[NSDateFormatter alloc] init]; 
    [self.outputDateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"]; 
} 


- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath: { 
    // dequeue or create the cell... 
    // ... 
    // ... 

    OLVisit *visit = [self.visits objectAtIndex:indexPath.row]; 
    NSString * strVisitDate = [self.outputDateFormatter stringFromDate:visit.olcreatedat]; 

    // Here you can assign the string to the right UILabel 
    cell.textLabel.text = strVisitDate; 

    return cell; 
} 

visit.olcreatedat是一個NSString

你想的visit.olcreatedat轉換爲格式EEEE,DD MMM YYYY HH:MM:SSž ?如果是這樣,我們來看看每一步。

首先,您需要將visit.olcreatedat字符串(存儲在strVisitDate中)轉換爲實際的NSDate對象visitDate。因此,爲了使這一步的工作,你需要接受visit.olcreatedat格式NSDateFormatter:YYYY-MM-DD HH:MM:SSž

然後,你要得到的visitDate的NSDate轉換成你喜歡的格式,所以你需要另一個NSDateFormatter,格式爲EEEE,DD MMM YYYY HH:MM:SSž

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Alloc the date formatter in a method that gets run only when the controller appears, and not during scroll operations or UI updates. 
    self.inputDateFormatter = [[NSDateFormatter alloc] init]; 
    [self.inputDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"]; 
    self.outputDateFormatter = [[NSDateFormatter alloc] init]; 
    [self.outputDateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"]; 
} 

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath: { 
    // dequeue or create the cell... 
    // ... 
    // ... 

    OLVisit *visit = [self.visits objectAtIndex:indexPath.row]; 
    NSString *strVisitDate = visit.olcreatedat; 
    NSDate *visitDate = [self.inputDateFormatter dateFromString:strVisitDate]; 
    strVisitDate = [self.outputDateFormatter stringFromDate:visitDate]; 

    // Here you can assign the string to the right UILabel 
    cell.textLabel.text = strVisitDate; 

    return cell; 
} 
+0

正是我想要做的。非常感謝你的回覆。 – motionpotion 2013-05-01 20:56:23

+0

只需要注意一點:如果您需要快速響應,請避免分配多個NSDateFormatter或更改日期格式,因爲這是一項繁重的操作。如果你在'tableView:cellForRowAtIndexPath:'中這樣做了,例如你可能會表現出惡劣的表現。在這種情況下,請改用一個或多個靜態NSDateFormatter,或重複使用存儲在屬性中的相同NSDateFormatter,就像您在此處所做的那樣。 – 2013-05-01 21:01:40

+0

感謝您的額外提示。它看起來像我做的正確,然後通過將NSDateFormatter設置爲我的ViewController屬性。 – motionpotion 2013-05-01 21:19:41

1

更改日期格式和替換代碼:

老:

[self.dateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"]; 

新:

[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"]; 

這是因爲使用的是不同的日期格式,而strVisitDate日期格式爲「yyyy-MM-dd HH:mm:ss z」。 所以你需要設置相同的格式。

編輯:

用這個代替你的代碼。

self.dateFormatter = [[NSDateFormatter alloc] init]; 
[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"]; 

OLVisit *visit = [self.visits objectAtIndex:indexPath.row]; 
NSString *strVisitDate = [NSString stringWithFormat:@"%@", visit.olcreatedat]; 

NSDate *visitDate = [dateFormatter dateFromString:strVisitDate]; 
strVisitDate = [dateFormatter stringFromDate:visitDate]; 

//Here you can set any date Format as per your need 

[dateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss z"]; 
strVisitDate = [dateFormatter stringFromDate:visitDate]; 

希望它可以幫助你。

+0

這幫助了很多,它的工作原理,但我怎麼才能得到我需要的格式的日期「EEEE,dd MM yyyy HH:mm:ss z」。 我需要它以特定的格式輸出到UILabel。 – motionpotion 2013-05-01 20:45:32

+0

檢查我的更新答案。 – 2013-05-01 20:52:49

+0

謝謝。這兩種答案適用於這種情況。 – motionpotion 2013-05-01 20:57:32