iphone - iPhone 中的 CST 时区从 11 月 3 日起增加一天吗?

标签 iphone ios ipad nsdate nsdateformatter

我尝试添加 11 月 3 日起的某一天,时区如达拉斯,但我做不到。

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.

请告诉我如何做到这一点,

提前致谢

最佳答案

问题出在 intime - 你必须使用格式化程序来初始化 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 会用代码写出这些类型的问题。 例如。在这种情况下,XCode 会警告您此问题:Incompatible pointer types initializing 'NSDate *__strong' with an expression of type 'NSString *'

关于iphone - iPhone 中的 CST 时区从 11 月 3 日起增加一天吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18126377/

相关文章:

iphone - 推送通知徽章不来

ios - 如何在ejabberd中设置用户密码?

ipad - 无法让 UISearchBar 范围栏出现在 iPad 上的工具栏(或任何地方)中

ios - Xamarin 在方向更改时形成 iOS 图像转到角落

php - 我应该如何让我的 PHP API 为 iPhone 应用程序工作

iphone - 在应用程序中购买 SKProductsRequest 在 ios 5 中崩溃我的应用程序

iphone - 如何将现有应用程序日志复制到 TestFlight 并使用宏

ios - 奇怪的 JSON API 响应 SWIFT

objective-c - 在 UIImagePickerController 中保存位置

iphone - 开始或停止输入字符时 UITextField 字体大小发生变化