How to parsing NSDate to RFC 822 always use in English? - objective-c

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];

Related

Custom date string to NSDate

I am trying to convert Mon, 01 Aug 2016 04:15 PM IST to NSDate
I tried using the below code but always stuck with nil. Please help
NSString *fullTime = #"Mon, 01 Aug 2016 04:15 PM IST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm a ZZZ"];
NSDate *tempTime = [dateFormatter dateFromString:fullTime];
You main issue is the IST in the date, IST is not a standard and can stand for many time zones:
IST Indian Standard Time UTC+05:30
IST Irish Standard Time UTC+01
IST Israel Standard Time UTC+02
The date formatter will not be able to correctly format the date since it does not know which timezone is meant.
If you can you should have the date changed or remove the IST part from the date.
Also you will need to add a locale to the date formatter you it knows in which language is used in the date string.
For english beste use en_US_POSIX:
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
I tried your code.First it gives nil.Then I changed LocaleIdentifier to en_IN because
IST stands for both Israel Standard Time, and India Standard Time also it indicates this
NSString *strDate = #"Mon, 01 Aug 2016 04:15 PM IST";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, dd MMM yyyy HH:mm a zzz"];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_IN"];
NSDate *dateStr = [dateFormat dateFromString:strDate];
NSLog(#"The date is - %#",dateStr);
The Printed Result is
The date is - 2016-08-01 06:45:00 +0000
Convert between date formats in Objective-C

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.

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"]];

Http Date to NSDate

I have a string taken from an http response header field "Date" in this format:
"Sun, 24 Jun 2012 16:34:51 GMT"
what i want is to convert this string in a NSDate object. For this scope I have instantiated an NSDateFormatter using various format:
[dateFormatter setDateFormat:#"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss 'GMT'"];
but when I print the string from date using:
[dateFormatter dateFromString:date]
I receive:
(null)
where am I doing wrong?
Not sure I understand the problem, as your own code seems to work fine for me:
NSString *dateStr = #"Sun, 24 Jun 2012 16:34:51 GMT";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(#"Date: %#", date);
Which gives this output:
Date: 2012-06-24 16:34:51 +0000
Unless I have misunderstood your question, this is the correct result.

parsing and format different rss pubdate

I'm creating a universal feed reader and i need to format the rss pubDate but the rss pubdate is always different, for example:
Wed, 25 May 2011 02:10:00 CEST
Wed, 25 May 2011 18:54:26 +00:00
Wed, 25 May 2011 08:13:22 +0000
Wed, 25 May 2011 14:21:54 GMT
26 May 2011 10:32:00 +0100
I tried to use this code:
NSString *dateString = #"Wed, 25 May 2011 18:54:26 +00:00";
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"EEE, dd MMMM yyyy HH:mm:ss +00:00"];
NSLocale *enLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[df setLocale:enLocale];
NSDate *date = [df dateFromString:dateString];
NSLog(#"'%#' = %#", dateString, date);
NSDateFormatter* df2 = [[[NSDateFormatter alloc] init] autorelease];
[df2 setDateFormat:#"EEE, dd MMMM yyyy HH:mm:ss"];
NSString *dateString2 = [df2 stringFromDate:date];
This code, however, works only with one type of rss pubDate, how can fix this problem?? how can create a universal dateformatter??
pubDate is in a date format, so you do the same format in your code to reflect that of the pubDate format.
EEE, dd MMMM yyyy HH:mm:ss +00:00 is equal to puDate: Wed, 25 May 2011 18:54:26 +00:00
You just have to compensate for the date format stored in pubDate.