ios - 将 "2011-11-04T18:30:49Z"转换为 "MM/dd/yy hh:mm:SS a"格式

标签 ios cocoa nsdate nsdateformatter

我需要转换以下格式的日期:“2011-11-04T18:30:49Z”

采用这种格式:“MM/dd/yy hh:mm:SS a”

添加了通过以下方式提取的用户系统 currentGMTOffset 偏移量:

NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];  
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];

任何帮助将不胜感激。

谢谢!

最佳答案

此代码片段将帮助您将格式化字符串转换为 NSDate。

    // Current date.
    NSString *currentDate = @"2011-11-04T18:30:49Z";

    // NSDateFormatter stuff.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

    // Convert to NSDate.
    NSDate *neededDate = [dateFormatter dateFromString:currentDate];

然后您只需转换为您请求的格式即可。

我使用 ARC,因此不需要内存管理。

关于ios - 将 "2011-11-04T18:30:49Z"转换为 "MM/dd/yy hh:mm:SS a"格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014836/

相关文章:

ios - UICollectionViewCell 中的渐变 BG

ios - Xcode 5.1 中的架构build设置不会改变

iphone - 导航栏和 UIWebview

ios - 24 小时制日期格式不正确

ios - 将完整日期转换为小时和分钟 - Swift

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

objective-c - For 语句每次都漏掉最后一个对象

ios - 需要大量的 SkSpriteNode 和 CPU 使用率已经达到 90%

Xcode 5 预定的 Bot 未集成

objective-c - NSSet 的 containsObject 是否为 : test for pointer equality or value equality?