iphone - NSDate到NSString的转换提供了不同的输出

标签 iphone objective-c ios nsdate nsdateformatter

可以重复一遍,但是我找不到正确的解决方案。

NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"YYYY-MM-dd"];
 NSString *dateString = [dateFormat stringFromDate:receiveddate];
 NSLog(@"Date: %@", dateString);

接收日期为NSDate,其值为 2012-06-11 00:00:00 +0000
我只想将上述日期转换为NSString,其中值应该只是 2012-06-11

但是当我检查出来时,它给了我 2012-00-11 00:00:00 不同的输出。真奇怪。我如何摆脱这个问题。

最佳答案

嗨,我使用的代码与您发布的代码相同,并且可以为您提供所需的正确输出。所以我认为您的接收日期格式存在问题。这是我正在使用的代码

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
    [dateFormat setDateFormat:@"YYYY-MM-dd"];
    NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
    NSLog(@"Date: %@", dateString);

OUTPUT:Date: 2012-06-06

关于iphone - NSDate到NSString的转换提供了不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10910498/

相关文章:

iphone - 核心图 : Allow horizontal scrolling in positive quadrant only

iphone - 更改父 View Controller 容器中的标签

iphone - iOS 中的 NSString 值验证

ios - 如何从函数内的解析查询生成的函数返回值?

iphone - 委托(delegate)未在 iOS5 StoryBoard UISplitViewController 中设置

ios - 调用 applicationDidBecomeActive 时如何通知事件 View Controller ?

ios - 更改高度时如何在单元格中显示标签 View 的全文

iphone - UIButton 阴影看起来不正确

objective-c - 我应该将托管对象上下文作为方法的参数包含在内吗?

ios - 几个UIPickerView