CST时区从11月3日开始在iphone中增加一天?

时间:2013-08-08 12:44:35

标签: iphone ios ipad nsdate nsdateformatter

我试图在11月3日添加一天像dallas这样的时区,但我不能这样做。

NSDate *intime = @"2013-11-03 05:00:00 +0000";

NSDate *nextday= [NSDate dateWithTimeInterval:24*3600 sinceDate:intime];

NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSString *convert=[formatter stringFromDate:nextday];
NSLog(@"convert:%@",convert);

Nslog值:

intime:  2013-11-03 05:00:00 +0000
nextday: 2013-11-04 05:00:00 +0000
convert: 11/03/2013 23:00:00 // but I need this 11/04/2013.

请告诉我,怎么做,

提前致谢

1 个答案:

答案 0 :(得分:1)

问题在于intime - 您必须使用formatter从字符串初始化NSDate。所以我测试的例子是:

//  NSDate *intime = @"2013-11-03 05:00:00 +0000"; // wrong

NSString *str =@"2013-11-03 05:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *intime = [formatter dateFromString:str];

NSDate *nextday= [NSDate dateWithTimeInterval:24*3600 sinceDate:intime];

NSDateFormatter *formatter2=[[NSDateFormatter alloc]init];
[formatter2 setDateFormat:@"MM/dd/yyyy"];
NSString *convert=[formatter2 stringFromDate:nextday];
NSLog(@"%@, %@, %@", intime, nextday, convert);

输出结果为:

  
    

2013-08-08 15:03:23.762 2013-11-03 05:00:00 +0000,2013-11-04 05:00:00 +0000, 11/04/2013

  

一个小评论:默认情况下,当显示问题时,XCode会在代码中写出这些问题(Preferences-> General-> Show live issues)。 例如。在这种情况下,XCode会警告您:Incompatible pointer types initializing 'NSDate *__strong' with an expression of type 'NSString *'