【问题标题】:NSDate in iOS6 not the same as in iOS5iOS6 中的 NSDate 与 iOS5 中的不同
【发布时间】:2013-06-15 04:52:18
【问题描述】:

我的这段代码适用于 iOS5,但我只是测试一下

NSDate *now = [NSDate date];
NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];

有谁知道重写这个的简单解决方案。日期的格式应该是 dd mm yyyy

【问题讨论】:

  • 您发布的代码导致在NSDate 对象上调用description 方法。 description 方法的输出仅用于调试,输出会随时间而变化(如您所见)。

标签: objective-c ios5 ios6 nsdate


【解决方案1】:

将日期格式化为字符串的正确方法是使用NSDateFormatter。您可以使用-setDateStyle: 方法将样式设置为适合用户当前语言环境的样式,或者使用-setDateFormat: 将格式设置为特定字符串。

【讨论】:

    【解决方案2】:
    Try this one , Definitely will get Proper solution:
    
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];//as per your requirement
    
    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
    [timeFormat setDateFormat:@"HH:mm:ss"]; //as per your requirement
    
    NSDate *now = [[NSDate alloc] init];
    
    NSString *theDate = [dateFormat stringFromDate:now];
    NSString *theTime = [timeFormat stringFromDate:now];
    
    NSLog(@"theDate:%@ "theTime:%@" , theDate, theTime);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多