2011-10-31 44 views
0

並且即時調用Web服務。在我的xml文件我有這樣嘗試在xcode'm中使用touchXML格式化日期

<a:DateIn>2011-10-28T08:12:58.36+02:00</a:DateIn> 

IM一行調用它,它出現在我的模擬器喜歡這樣2011-10-28T08:12:58.36 + 02:00

我在Xcode代碼這樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView 
     dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
      reuseIdentifier:@"MyIdentifier"]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

NSDictionary *item = (NSDictionary *)[blogEntries 
      objectAtIndex:indexPath.row]; 
cell.textLabel.text = [item objectForKey:@"a:DateIn"]; 
cell.detailTextLabel.text = [item objectForKey:@"a:Description"]; 
cell.detailTextLabel.font=[UIFont fontWithName:@"Arial" size:12.0]; 

return cell; 
} 

洛斯我希望將日期轉換月:日小時:分鐘

+0

看到我的帖子在這[鏈接](http://stackoverflow.com/questions/7873690/need-help-for-specific-date-format/7874076#7874076)。 – Ilanchezhian

+0

我已經更新了我的答案。試試看是否有效。 – Ilanchezhian

+0

即時消息顯示的實際時間,但我想顯示從DateIn的時間? – user993074

回答

0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView 
      dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
       reuseIdentifier:@"MyIdentifier"]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    NSDictionary *item = (NSDictionary *)[blogEntries 
       objectAtIndex:indexPath.row]; 

    //cell.textLabel.text = [item objectForKey:@"a:DateIn"]; 
    //NSString *dateString = @"2011-10-28T08:12:58.36+02:00"; 
    NSString *dateString = [item objectForKey:@"a:DateIn"]; 
    //To convert the string to NSDate 

    NSRange range = [dateString rangeOfString:@":" options:NSBackwardsSearch]; 
    dateString = [dateString stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:range]; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"yyyy-MM-ddEHH:mm:ss.SSSZ"]; 
    NSDate *date = [dateFormat dateFromString:dateString]; 
    [dateFormat release]; 

    NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init]; 
    [dateFormat1 setDateFormat:@"MM:dd HH:mm"]; 
    NSString *dateString1 = [dateFormat1 stringFromDate:date]; 
    [dateFormat1 release]; 

    //dateString1 is the required string. 
    cell.textLabel.text = dateString1; 


    cell.detailTextLabel.text = [item objectForKey:@"a:Description"]; 
    cell.detailTextLabel.font=[UIFont fontWithName:@"Arial" size:12.0]; 

    return cell; 
} 
+0

我不明白我應該把這個在代碼中,我不能看到它應該如何工作... – user993074

+0

我已更新我的答案,以便您可以輕鬆地瞭解我的答案。 – Ilanchezhian

+0

它的工作非常感謝你mucj,但我得到錯誤的時間,我應該得到08:15,但我得到00:15? – user993074

0
NSString *dateform; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

dateform = [NSString stringWithFormat:@"%@",[object objectForKey:@"a:DateIn"]]; 
NSLog(@"before dateform %@",dateform); 
dateform = [self dateFormater:dateform]; 
NSLog(@"afters dateform %@",dateform); 
} 


-(NSString *)dateFormater:(NSDate *)oldDate 
{ 
     //NSDate *date1 = [[invoice objectAtIndex:0] valueForKey:@"date"]; 
     NSString *dateStr =[NSString stringWithFormat:@"%@",oldDate]; 
     NSDateFormatter *dtF = [[NSDateFormatter alloc] init]; 
     [dtF setDateFormat:@"YYYY-MM-dd HH:mm:ss +0000"]; 
     NSDate *d = [dtF dateFromString:dateStr]; 
     NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init]; 
     [dateFormatStr setDateFormat:@"d-MMM-yyyy"]; 
     NSString *strDate = [dateFormatStr stringFromDate:d]; 
     [dtF release]; 
     [dateFormatStr release]; 
     return strDate; 
    } 
+0

我應該在哪裏放置一個:DateIn? – user993074

+0

dateform = [NSString stringWithFormat:@「%@」,[object objectForKey:@「a:DateIn」]]; dateform = [self dateFormater:dateform]; –

+0

我不能得到這個工作,你可以編寫更具體的代碼,或者你可以再次編寫整個代碼,包括日期格式? – user993074