ios - 使用上午/下午和时区将 NSString 转换为 NSDate

标签 ios objective-c nsdate nsdateformatter nstimezone

我的输入 NSString2017-01-11 5:33:55 am +0000。我的代码是:

    date = @"2017-01-11 5:33:55 am +0000";
    uploadFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss a Z";
    [uploadFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    NSDate *theDate = [uploadFormatter dateFromString:date];

但是返回的 NSDate 总是 2017-01-11 0:33:55 UTC 而不管输入的时间。这意味着如果输入是 2017-01-11 3:33:55 am +0000,结果也是一样的。

有没有人遇到过这个问题?任何想法将不胜感激。

最佳答案

HH 是 24 小时格式的四小时,但由于您有“am”/“pm”,所以它显然是 12 小时格式。 将 dateFormat 中的 HH 更改为 hh

请记住可用的文档 here对于格式,对于 iOS7+ 是 here .

关于ios - 使用上午/下午和时区将 NSString 转换为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41587287/

相关文章:

ios - StoryBoards 从 View Controller 导航到 Tab view controller iOS 5

iphone - 切换背景图片 iphone 5 - iphone 4

ios - 当前事件的时间戳/NSDate在UIKit中启动

ios - 如何禁用导航栏动画?

ios - 如何在 RxSwift 中为 `never` 运算符触发事件

ios - 我们如何改变 EditingStyle Button 相对于 tableVIew 单元格的高度

iOS - 删除 UITabBarViewController 阴影

objective-c - #define 在 Swift 中是如何工作的?

ios - 在西类牙语语言环境中将 NSString 转换为 NSDate

iphone - Apple 在哪个 WWDC session 视频中展示了用于本地化时间间隔格式化的 API?