Conversion from NSString to NSDate and back again - objective-c

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.

Related

Set date format Objective-C

I want to convert this string "Sat, 01 Aug 2015 21:03:59 GMT" to NSDate object
Here's my code
+(NSDate *)getDateFromDateString :(NSString *)dateString {
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;}
but date always is nil. I guess something wrong with date format. Can someone please give me some advice?
Thanks for your support, i found out the answer. Need to declare the locale
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Normally this is how I would call a utility function:
DateHelperClass.h
+ (NSDate *)getDateFromDateString :(NSString *)dateString;
DateHelperClass.m
+ (NSDate *)getDateFromDateString :(NSString *)dateString
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
return date;
}
SomeOtherClass.m
#import "DateHelperClass.h"
- (void)convertDate
{
NSLog(#"%#",[DateHelperClass getDateFromDateString:#"Sat, 01 Aug 2015 21:03:59 GMT"]);
}
Result:
2015-08-01 21:03:59 +0000

How to parsing NSDate to RFC 822 always use in English?

I need to PUT a RESTful to server.
the Date must use like Sat, 19 Jan 2013 04:09:58 GMT.
in objc I wrote this:
NSDateFormatter* _GMTDateFormatter = [[NSDateFormatter alloc] init];
[_GMTDateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss"];
[_GMTDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* theDate = [_GMTDateFormatter stringFromDate:[NSDate date]];
theDate = [theDate stringByAppendingString:#" GMT"];
NSLog(#"%#",theDate);
it will be output Sat, 19 Jan 2013 04:09:58 GMT
but on my real device,the language is Chinese,it will output 周六, 19 1月 2013 04:09:58 GMT.
How to make it always use in English?
You need to set the locale of the NSDateFormatter object.
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier: #"en_US"];
[_GMTDateFormatter setLocale: usLocale];

NSDate coming null in iOS

I have to get a date from a string. So I am using the below code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//Fri Mar 09 15:31:35 CST 2012
[formatter setDateFormat:#"EEE MMM d HH:mm:ss ZZZ yyyy"];
NSDate *date = [formatter dateFromString:[newDict objectForKey:#"eventDate"]];
NSLog(#"aDate is %#",[newDict objectForKey:#"eventDate"]);
eventDate is coming in the format aDate is Fri May 18 12:00:37 IST 2012 but date is coming null.
I know it is a small issue but I am really stuck up with it.
Your input string can be parsed with a format of #"EEE MMM d HH:mm:ss VVV yyyy".
Try adding this before the dateFromString: call:
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"]];

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

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 !

How to Format Unknown Date Format in Objective-C

I've probably overlooked something simple, but I can't figure out how to convert a specific date format in Objective-C. I'm receiving dates in this format:
Sun, 10 Oct 2010 01:44:00 +0000
And I need to convert it to a long format with no time, like this:
October 10, 2010
I've tried using NSDateFormatter, but when I try to get a date from the string, it always comes back nil. I've tried it this way:
NSString *dateFromData = [articleData objectAtIndex:5];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [dateFormatter dateFromString:dateFromData];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
The date string always comes back nil. Am I missing something simple here?
This is the code I got to work, couldn't get the other format string to function
NSString *dateFromData = #"Sun, 10 Oct 2010 01:44:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss zzzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate * date = [dateFormatter dateFromString:dateFromData];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
The problem is you need to use two date formatters. One to parse the original format and one to produce the desired output. You also can't use the LongStyle date format as it doesn't match your input style.
NSDateFormatter *df = [[NSDateFormatter alloc] initWithDateFormat:#"%a, %d %b %Y %H:%M:%S %z"
allowNaturalLanguage:false];
NSDate *d = [df dateFromString:#"Sun, 10 Oct 2010 01:44:00 +0000"];
NSDateFormatter *df2 = [[NSDateFormatter alloc] initWithDateFormat:#"%B %d, %Y"
allowNaturalLanguage:false];
[df2 stringFromDate:d]
(Note, I wrote this code in MacRuby and ported it back to Objective-C so there maybe syntax errors.)
irb(main):001:0> df = NSDateFormatter.alloc.initWithDateFormat("%a, %d %b %Y %H:%M:%S %z", allowNaturalLanguage:false)
=> #<NSDateFormatter:0x20021b960>
irb(main):002:0> d = df.dateFromString("Sun, 10 Oct 2010 01:44:00 +0000")
=> #<NSCalendarDate:0x2002435e0>
irb(main):004:0> df2 = NSDateFormatter.alloc.initWithDateFormat("%B %d, %Y", allowNaturalLanguage:false)
=> #<NSDateFormatter:0x20026db60>
irb(main):005:0> df2.stringFromDate(d)
=> "October 09, 2010"