简体   繁体   中英

nsdateformatter — convert string to date

I can't seem to get this right. here is the original value I am receiving... sDateOG NSString * @"2015-02-24T11:06:10.847-05:00" 0x7cd09580

I am trying to turn that into nice and clean date/timestamp.

Still new at this, but what I thought was I should change this to a NSDATE, then I have options for the final format?

I can't seem to make this an nsdate though. this is what I am trying...

    NSString *sDateOG = [variableArray objectAtIndex:1];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'S''Z'"];

anyone see what I am doing wrong?

您需要考虑小数秒(并且不要引用任何需要特殊字符的字符。)此格式字符串应该可以工作:

"yyyy'-'MM'-'dd'T'HH':'mm':'S.SSSZZZZZ"

Why are you putting everything keyword (yyyy, MM, etc...) between quotes ? You must also put as many keyword as digits/characters (hence the ZZZZZ).

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SSSZZZZZ"];

EDIT : Removed trailing useless quote

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM