Ios 转换;2011-11-04T18:30:49Z“;至;年月日hh:MM:SS a“;格式

Ios 转换;2011-11-04T18:30:49Z“;至;年月日hh:MM:SS a“;格式,ios,cocoa,nsdate,nsdateformatter,Ios,Cocoa,Nsdate,Nsdateformatter,我需要从以下格式转换日期:“2011-11-04T18:30:49Z” 此格式为:“MM/dd/yy hh:MM:SS a” 加上用户系统当前GMTOFFSET偏移量,通过以下方式提取: NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone]; NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT]; 任何帮助都将不胜感激 谢谢 看看NSDateFormatter。

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

此格式为:“MM/dd/yy hh:MM:SS a”

加上用户系统当前GMTOFFSET偏移量,通过以下方式提取:

NSTimeZone* currentTimeZone = [NSTimeZone localTimeZone];  
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMT];
任何帮助都将不胜感激


谢谢

看看NSDateFormatter。您需要在日期格式化程序对象上设置日期格式和时区,然后才能将字符串转换为日期,将日期转换为字符串。

此代码段将帮助您将格式化的字符串转换为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];
然后你只需要转换成你所要求的格式

Im使用ARC,因此无需内存管理。

+ (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
 Returns a user-visible date time string that corresponds to the specified
 RFC 3339 date time string. Note that this does not handle all possible
 RFC 3339 date time strings, just one of the most common styles.
 */

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);

    [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;
}


这可能会有所帮助。

在过去两个小时里,我一直在尝试这样做:(好的。那么,什么给你带来了麻烦?格式字符串?请查看此参考以了解要使用的代码()。您需要使用用于转换为NSDate的原始格式设置一个日期格式化程序,然后使用另一种格式设置第二个格式化程序,将NSDate转换为所需的字符串。这就是我的理解:NSDateFormatter*df=[[NSDateFormatter alloc]init];[df setDateFormat:@“YYYY-MM-DDTHH:MM:ss Z”];NSDate*createdAtDate=[df dateFromString:createdAtString];其中createdAtString是:“2011-11-04T18:30:49Z”但这会将createdDate设置为nullOk,但会创建一个时区为0的日期,这是可以的,因为原始日期的偏移量为0。但根据用户的不同,我需要将日期设置为用户区域偏移量,例如+5或-1。您已经给了我所需答案的一半:)谢谢,伙计。谢谢,我基本上需要找出从服务器接收到时间后的过去时间,我只是得到了当前系统时间,并简单地使用了NSTimeInterval interval=[currentDate TimeIntervalsIncestate:neededDate]