试图格式化日期

时间:2011-10-31 08:06:22

标签: iphone objective-c xcode4

在xcode中使用touchXML并调用web服务。在我的xml文件中,我有一个像这样的行

<a:DateIn>2011-10-28T08:12:58.36+02:00</a:DateIn>
我打电话给它,它出现在我的模拟器中就像这样2011-10-28T08:12:58.36 + 02:00

我的代码在xcode loos中就像这样

    - (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;
}

我想将日期转换为月份:日期:分钟

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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;
}