开发者

Formatting an NSDate

开发者 https://www.devze.com 2022-12-12 12:20 出处:网络
I am pulling data from an RSS Feed. One of the keys in the feed is is a string representing the date and time the item was created.

I am pulling data from an RSS Feed. One of the keys in the feed is is a string representing the date and time the item was created.

I am trying to convert this string value to an NSDate. The string value is returned from the RSS feed as: 2009-11-18T22:08:00+00:00

I tried the following code to no avail:

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFor开发者_如何学运维mat:@"yyyyMMdd HH:mm"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

Ideally; on top of converting the value to a NSDate value, I would also like to format it using the localised date format on the handset.

Any pointers would be a great help.

Kind Regards


To retrieve the dateFormat:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: @"UsersDate"]];

You can use the predefined formats if you would like to format time and date according to the user's locale:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"%@",[dateFormatter stringFromDate:someDate]);
[dateFormatter release]; // don't forget to release the dateformatter    

Check the documentation to see which formatter suits you best.


Actually date form in RSS feed may differ, so best way to parse it is following

[NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822];

This is not a standard function, library for it can be found here https://gist.github.com/953664

0

精彩评论

暂无评论...
验证码 换一张
取 消