2012-11-21 35 views
0

可能重复:
NSDateFormatter not giving me correct的NSDate和NSDateFormatter不一致

我使用的NSDate我使用以下方法转换为只日期:

+ (NSDate *) dateOnly:(NSDate *)date 
{ 
    long time = [date timeIntervalSinceReferenceDate]; 
    long timeMod = time % kNumSecondsInDay; 
    NSTimeInterval newTime = time - timeMod; 

    return [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:newTime]; 
} 

这给我预期的结果:

NSLog(@"Due Date:%@", _dueDate); 

- >截止日期:2012年11月21日00:00:00 +0000

然而,当我使用的日期格式,它给了我一天以前:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateStyle:NSDateFormatterShortStyle]; 
    NSString *result = [formatter stringFromDate:_dueDate]; 
    NSLog(@"Result: %@", result); 

- >结果:1​​1/20/12

是什么给出的?

+1

时区是什么给。 (重复约100其他“NSDateFormater不起作用”的线程。) –

+0

我看到了问题,谢谢。 – Destron

回答

3

时区。日期是UTC 11/21午夜;如果您的时区在UTC后面,那么它将是11/20。

+0

我看到问题了,谢谢。 – Destron

相关问题