Set date format Objective-C - 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

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.

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.

Dateformatter change the year of my date

I try to display a date with a dateformatter but this one change the year of the date.
This is the method :
setLabelWithDate:(NSdate *)date{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"dd MMMM YYYY"];
NSLog(#"Date : %#",date);
NSLog(#"stringFromDate %#",[dateFormatter stringFromDate:date]);
self.selectedDateLabel.text = [dateFormatter stringFromDate:date];
[dateFormatter release];
}
And this is what is displayed in the console :
2012-05-04 13:08:50.442 MyAppli[3339:fb03] Date : 2012-12-30 00:00:00 +0000
2012-05-04 13:08:50.443 MyAppli[3339:fb03] stringFromDate 30 December 2013
Here I m. The dateFormatter add myDate one year and I really don't know why. This case happens only for the 2 last dates of the year (30 and 31 december).
Thanks for your help.
Alexandre
Use
[dateFormatter setDateFormat:#"dd MMMM yyyy"]; // lowercase y
and you probably should check this and this.

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"