how to display the date and time from rss feed in iphone - objective-c

I am getting data from the rss feeder. The data contains title, description and date & time.
I'm getting the date as "Thu, 21 Apr 2011 14:23:41 +0100" from rss feeder. I wanted to remove +0100 from the date and display it. Can any one help me out with this.
Thanks in advance.

I would recommend to take another approach. Use a NSDateFormatter to parse this date, and output it with another formatter. As you might know there are people living in non-english countries, and most likely they like their dates formatted in their local style.
And if the reader of the feed is in a different timezone than the server (or the author?) the time will be wrong anyway.
Something like this will display the actual date and time in the local format. I would recommend to use something similar.
NSString *str = #"Thu, 21 Apr 2011 14:23:41 +0100";
NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [inputFormatter dateFromString:str];
NSDateFormatter *outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[outputFormatter setDateStyle:NSDateFormatterLongStyle];
[outputFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *outStr = [outputFormatter stringFromDate:date];

Here how you can do that
NSString * str = #"Thu, 21 Apr 2011 14:23:41 +0100";
[str substringToIndex:[str length] - 6];
why [str length] - 6] because the length of " +0100" is 6.
Good Luck !

Related

Conversion from NSString to NSDate and back again

I know this question has been asked before (most notably here: Converting NSString to NSDate (and back again)), but I am having difficulties. I am using an RSS feed parser which returns a date in this format:
Fri, 23 Nov 2012 15:39:00 -0800
I wish to convert it to this format:
Nov. 23 2012
WIth a separate time formatted like this:
3:39:00 PM
Here is the code I currently have:
NSString *RSSDate1=#"Fri, 23 Nov 2012 15:39:00 -0800";
NSDateFormatter *RSSDateFormatter = [[NSDateFormatter alloc] init];
[RSSDateFormatter setDateFormat:#"D, d M Y H:i:s O"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [RSSDateFormatter dateFromString:RSSDate1];
[RSSDateFormatter release];
NSDateFormatter *RSSDateFormatter2=[[NSDateFormatter alloc] init];
[RSSDateFormatter2 setDateStyle:NSDateFormatterShortStyle];
UIAlertView *dateAlert=[[UIAlertView alloc] init];
[dateAlert setTitle:[RSSDateFormatter2 stringFromDate:dateFromString]];
[dateAlert addButtonWithTitle:#"Dismiss"];
[dateAlert show];
[dateAlert release];
Right now, the string is returning nil, so I'm betting my formatting is off. Any help would be appreciated. Thanks!
You might try using this configured NSDateFormatter to convert the current date (now) to an NSString and print out the string. Look at the format that's output and compare it to the format you want. The difference(s) should clue you in as to what you need to change to get your desired format.
Change it to,
NSString *RSSDate1 = #"Fri, 23 Nov 2012 15:39:00 -0800";
NSDateFormatter *RSSDateFormatter = [[NSDateFormatter alloc] init];
[RSSDateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzzzz"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [RSSDateFormatter dateFromString:RSSDate1];
NSLog(#"dateFromString = %#", dateFromString);
[RSSDateFormatter release];
NSDateFormatter *RSSDateFormatter2 = [[NSDateFormatter alloc] init];
[RSSDateFormatter2 setDateFormat:#"MMM dd, yyyy hh:mm aaa"];
NSLog(#"dateToString = %#", [RSSDateFormatter2 stringFromDate:dateFromString]);
UIAlertView *dateAlert = [[UIAlertView alloc] init];
[dateAlert setTitle:[RSSDateFormatter2 stringFromDate:dateFromString]];
[dateAlert addButtonWithTitle:#"Dismiss"];
[dateAlert show];
[dateAlert release];
Output:
dateToString = Nov 23, 2012 03:39 PM
Note that the format for "Fri, 23 Nov 2012 15:39:00 -0800" is "EEE, dd MMM yyyy HH:mm:ss zzzzz" and for "Nov 23, 2012 03:39 PM" it is "MMM dd, yyyy hh:mm aaa". Each letters in the format represents the corresponding word in date string. For eg:- EEE is for Fri, aaa for PM etc.. Check this for more information on how to do this formatting.

Need to convert custom timestamp to different format in Objective-C iOS

I have a string "2012-06-04" and am having a hard time converting it to: June 4, 2012.
Is there a quick way to transform this? I come from a ruby world where you would convert everything to seconds and that back out to the format you need it. Is there a reference that shows how to do that?
Thanks
One way to do so is to convert the string to an NSDate using the NSDateFormatter with the 2012-06-04 format, and then convert the NSDate back to a string using the June 4, 2012 format:
NSString* input = #"2012-06-04";
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate* date = [df dateFromString:input];
[df setDateFormat:#"MMMM d, yyyy"];
return [df stringFromDate:date];
The format string's syntax is described in UTS #35.
Something like
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
fromDateFormatter.dateFormat = #"yyyy-MM-dd";
NSDate *date = [fromDateFormatter dateFromString:#"2012-06-04"];
NSLog(#"%#", date);
NSDateFormatter *toDateFormatter = [[NSDateFormatter alloc] init];
toDateFormatter.dateFormat = #"MMMM d, yyyy";
NSString *toDate = [toDateFormatter stringFromDate:date];
NSLog(#"%#", toDate);
#=> 2012-05-30 20:51:12.205 Untitled[1029:707] 2012-06-03 23:00:00 +0000
#=> 2012-05-30 20:51:12.206 Untitled[1029:707] June 4, 2012
To work this out you can use Apple's class references NSDateFormatter and other sources like this IPHONE NSDATEFORMATTER DATE FORMATTING TABLE and some trial and error

Use (Billy Meltdown) NSDate-helper methods to convert Unix timestamp and display?

I would like to convert a Unix timestamp e.g 1315401371 stored as a NSString into a date format of 23:00 12 October 2011.
There are a number of similar questions being ask in relation to this however I wish to use billymeltdown / nsdate-helper methods
I have imported the 2 files (.h and .m) into my project, but have not manage to get further than that.
There is documentation, which I have read, however I am still very new to objective C and do not understand how I actual use the libraries.
Thanks in Advance.
You should use NSDateFormatter
NSTimeInterval timestamp = 1315401371;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm dd MMMM yyyy"]; // use one d for 7 October instead of 07 October
// For english month names
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
[usLocale release];
NSString *dateString = [dateFormatter stringFromDate:date];
[dateFormatter release];

Save NSDate to CoreData

i want to save NSDate from XML to coreData.
I dont know how to set the Value for NSDate. Can someone give me little help?
My Entity attribute is NSDate. Or should it be a String and Save the Date as String??
[Sets setValue:[TBXML textForElement:xmlDate] forKey:#"beginDate"];
Thanks for any response and help,
brush51
Whether you are intentionally obfuscating or not, you don't say it, so I'm going to guess that:
[TBXML textForElement:xmlDate] returns an NSString
beginDate is a Date property of entity Sets
In which case your code is obviously incorrect: you must pass an instance of NSDate to setValue:forKey:. Your code would look like:
NSDateFormatter *myXMLdateReader = [[NSDateFormatter alloc] init];
[myXMLdateReader setDateFormat:#"yyyy-MM-dd"]; // for example
NSDate *itsDate = [myXMLdateReader dateFromString:[TBXML textForElement:xmlDate]];
[myXMLdateReader release];
[Sets setValue:itsDate forKey:#"beginDate"];
It is no problem to add NSDate to CoreData. All you need to do is to convert NSString from XML file to NSDate object. For converting NSString <-> NSDate you can use NSDateFormatter class. In example below you can see how I do this:
NSDateFormatter *parseFormatter = [[[NSDateFormatter alloc] init] autorelease];
[parseFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"US"] autorelease]];
[parseFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSString *dateString = #"Mon, 02 May 2011 21:12:56 +0000";
NSDate *dateToAdd = [parser.parseFormatter dateFromString:dateString];
You can set needful date format and use this code in your project.

NSDateFormatter and date conversion not working

I am developing an app for the iPhone where I need to convert an date from an XML feed into just a HH:MM format.
I have the following method that doesn't work and I have no clue what I am doing wrong.
As an example, the timeToConvert string would be: "Mon, 01 Feb 2010 21:55:00 +0100" (without the quotes)
The method works when the region is set to US (I get back the correct date), but not when I change the region (in Settings->General->International) to Spain, or other regions (in that case I get back nil).
- (id)timeConvertToHHMM:(NSString *)timeToConvert {
NSString *newPubDate = timeToConvert;
//Let's remove any rubbish from the code
newPubDate = [newPubDate stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//create formatter and format to convert the XML string to an NSDate
NSDateFormatter *originalDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[originalDateFormatter setDateFormat:#"EEE, d MMM yyyy H:mm:ss z"];
//run the string through the formatter
NSDate *formattedDate = [[NSDate alloc] init];
formattedDate = [originalDateFormatter dateFromString:newPubDate];
//Let's now create another formatter to take the NSDate and convert format it to Hours and minutes
NSDateFormatter *newDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[newDateFormatter setDateFormat:#"HH:mm"]; // 24H clock set
// And let's convert it back to a readable string
NSString *calcHHMM = [newDateFormatter stringFromDate:formattedDate];
NSLog(#"CalcHHMM: %#", calcHHMM);
return calcHHMM;
}
Any hint on why this is not working, and just returning NULL will be more than welcome.
Problem appears to be your region setting is not "en-US" so the date formatter doesn't parse the string using the en-US format supplied. Although there may be a more elegant, general solution, doing a setLocale on originalDateFormatter to en_US can be used as a workaround to solve the problem.
As you've already tried in your code:
[originalDateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
I had the exact same issue. My problem was that my initial date string had a single millisecond character:
Example: 2011-02-06 08:13:22:1
and was being parsed with this format :[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
The iPhone simulator was forgiving and successfully parsed the date with the milliseconds, however when building to my iphone it did not.
Changing the formatter to: [formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.S"]; solved the problem.