Hey, guys i 开发者_StackOverflow社区searched a lot but cant get my code working.
I am getting a date in form of a string with format like : 2011-04-18 18:57:47.453000
I want to convert this to NSDate to calculate time elapsed, but I cant seem to get the conversion right. here is what i am doing:
NSString *dateString = [entityData objectForKey:@"creation_time"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.fff"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
when i print dateString i get : 2011-04-18 18:57:47.453000 but printing dateFromString gives: (null)
Can anyone point me in the right direction?
You probably solved this awhile ago... but:
NSString *dateString = @"2011-04-18 18:57:47.453000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone: [NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSS"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [df dateFromString:dateString];
[df release];
Some good information on converting date components can be found in the "Date Field Symbol Table" located here:
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
精彩评论