【问题标题】:NSDateFormatter Not Formatting StringNSDateFormatter 没有格式化字符串
【发布时间】:2013-05-02 06:22:33
【问题描述】:

我在 cellForRowAtIndexPath 中为 UITableView 调用了以下代码。

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-04-09 11:55:25 +0000”。

我得到一个 nil NSDate 值返回到 visitDate。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 如果您将日期格式设置为与日期格式相匹配,您的运气会更好。
  • visit.olcreatedat的类型是什么?是NSString吗?如果是这样,请摆脱对stringWithFormat:的调用。
  • @HotLicks 该链接也有帮助,我之前在发布之前已经通读过它,但它不能解决我将日期格式化为“EEEE,dd MMM yyyy HH:mm”格式的问题:ss z"
  • @rmaddy visit.olcreatedat 在我的访问实体中定义为日期数据类型。在上面的场景中,我从核心数据中拉回了这个字段。

标签: ios nsdateformatter


【解决方案1】:

更新:正如@rmaddy 所指出的,对visit.olcreatedat 是什么存在误解。 第一个答案是基于visit.olcreatedat 是 NSD日期。对于假设 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 z 格式吗?如果是这样,让我们​​完成每个步骤。

首先,您需要将visit.olcreatedat 字符串(存储在strVisitDate)转换为实际的NSDate 对象visitDate。因此,要使这一步生效,您需要一个接受 visit.olcreatedat 格式的 NSDateFormatter:yyyy-MM-dd HH:mm:ss z

然后,你要将得到的visitDateNSDate转换成你喜欢的格式,所以你需要另一个NSDateFormatter,格式为EEEE, dd MMM yyyy HH:mm:ss z

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

【讨论】:

  • 正是我想要做的。非常感谢您的所有回复。
  • 附注:如果您需要快速响应,请避免分配许多 NSDateFormatter 或更改其日期格式,因为这是一项繁重的操作。例如,如果你在tableView:cellForRowAtIndexPath: 中这样做,你的表现可能会很糟糕。在这种情况下,请改用一个或多个静态 NSDateFormatter,或者重用存储在属性中的相同 NSDateFormatter,就像您在此处所做的那样。
  • 感谢您的额外提示。看起来我做得对,然后通过将 NSDateFormatter 设置为我的 ViewController 上的属性。
  • 是的,但是您是在 viewDidLoad 还是 init 方法中将日期格式化程序分配给该属性?
  • 不,每次我需要使用它时,我都会将日期格式化程序分配给属性......在这种情况下是在 cellForRowAtIndexPath 中。我在视图控制器的其他方法中使用它,但每次我都设置它。我想你的意思是我应该在 init 或 viewDidLoad 中设置一次,然后在其他地方重用它而不再次设置。
【解决方案2】:

更改日期格式并替换代码:

旧:

[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];

希望对你有帮助。

【讨论】:

  • 这很有帮助,而且很有效,但是我如何将日期转换为我需要的格式“EEEE,dd MM yyyy HH:mm:ss z”。我需要它以特定格式输出到 UILabel。
  • @NishantTyagi,只是一个旁注:更改 dateFormat 是一项繁重的操作。例如,如果您在 tableView:cellForRowAtIndexPath: 中执行此操作,您可能会遇到缓慢的滚动性能,因此最好有一个具有正确格式的单独 NSDateFormatter 实例。
  • @AlessandroOrrù 你有我应该在 cellForRowAtIndexPath 之外执行此日期格式的示例吗?我目前正在使用这种方法执行此操作。我目前只返回几个单元格,但最终可能会返回数千个单元格,具体取决于用户选择的过滤器。
  • @motionpotion 我已经用一个例子更新了我的答案,看看它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多