可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
If I use the following code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@\"yyyy-MM-dd\'T\'HH:mm\"];
NSDate *myDate = [dateFormatter dateFromString:@\"2010-01-28T15:22:23.863\"];
NSLog(@\"%@\", [dateFormatter stringFromDate:myDate]);
It is successfully converted to a Date
object, however, I cannot seem to format it any other way than yyyy-MM-dd\'T\'HH:mm
, i.e. what gets logged is 2010-01-28T15:22:23
If I change the dateFormat to say [dateFormatter setDateFormat:@\"yyyy-MMMM-d\'T\'HH:mm\"];
the Date object is null...
So my ultimate question is how to format an ISO8601 timestamp from a SQL database to use, for instance, NSDateFormatterMediumStyle
to return \"January 1, 2010\"?
回答1:
You need another formatter to handle the output. Put this after your code:
NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];
[anotherDateFormatter setDateStyle:NSDateFormatterLongStyle];
[anotherDateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@\"%@\", [anotherDateFormatter stringFromDate:myDate]);
回答2:
I have a similiar but slightly more complex problem, and I\'ve found a very simple solution!
The problem:
My incoming ISO8601 dates look like this: 2006-06-14T11:06:00+02:00
They have a timezone offset at the end.
The solution:
Use Peter Hosey\'s ISO8601DateFormatter which you can download from here.
ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSDate *theDate = [formatter dateFromString:dateString];
[formatter release], formatter = nil;
and
ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSString *dateString = [formatter stringFromDate:[twitch dateTwitched]];
[formatter release], formatter = nil;
回答3:
Here is sample code from apple
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@\"yyyy\'-\'MM\'-\'dd\'T\'HH\':\'mm\':\'ss\'Z\'\"];
NSDate *date = [formatter dateFromString:dateString];
link:https://developer.apple.com/library/ios/#qa/qa1480/_index.html
回答4:
Just to mention that since iOS 6.0 you can use this format:
yyyy-MM-dd\'T\'HH:mm:ssZZZZZ
回答5:
I have a very fast C implementation of parsing ISO8601 NSStrings to NSDates in SAMCategories. You can install it with CocoaPods or just copy the files to your project.
Here\'s how to use it:
[NSDate sam_dateFromISO8601String:@\"2013-08-24T06:51:21-07:00\"]
It\'s very robust and supports missing timezones, etc. Like my comment above said, NSDateFormatter was very slow for me when parsing lots of dates. Switching to this implementation helped an immense amount.
This code is used in 6 production apps (including Hipstamatic and Roon) that I\'ve worked on with no known issues. There is a test suite to prevent regression.
In short, I think this is the best way to parse ISO8601 strings in Objective-C.
A short aside, if you\'re concerned about performance and transfer, sending integers instead of ISO8601 strings is greatly preferred. You can simply convert them into dates with [NSDate dateWithTimeIntervalSince1970:yourInteger]
. Performance is as fast as you can make it and there are less characters to transfer over the wire.
回答6:
I should note that last time I checked Apples NSDate
methods didn\'t support ISO8601. I still have a bug with Apple about putting official support in.
+ (id)dateWithNaturalLanguageString:(NSString *)string
will properly (last time I ran it) parse and create an NSDate
object from an ISO801 string, though the documentation says you shouldn\'t use it, it\'s worked on all ISO8601 dates I\'ve tried so far.
回答7:
@danielbeard answer worked for me. However you need to include the timezone otherwise it converts a UTC date into your current timezone which isn\'t what you want.
- (NSDate *) dateFromISO8601DateString:(NSString *) dateString {
NSDateFormatter * dateFormatter = [NSDateFormatter new];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@\"UTC\"];
[dateFormatter setDateFormat:@\"yyyy\'-\'MM\'-\'dd\'T\'HH\':\'mm\':\'ss\'Z\'\"];
NSDate * date = [dateFormatter dateFromString:dateString];
return date;
}
回答8:
@Thread captain
Try this Format : \"yyyy-MM-dd\'T\'HH:mm:ss.SSS\"