NSDate coming null in iOS - objective-c

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

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

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

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.

NSDateFormatter will not parse string with timezone that includes colon

I'm trying to transform #"Fri, 26 Aug 2011 10:51:00 +02:00" into an NSDate:
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
I get nil as a result; what am I doing wrong?
You can use the getObjectValue:forString:range:error: method to parse dates that have the colon in the timezone:
// test date formatting
NSString *dateString = #"2012-04-11T18:34:19+00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:dateString range:nil error:&error];
The colon in the timezone (+02:00) is the issue. According to the Unicode Standard #35, 1..3 capital Z pattern denotes a RFC 822 time zone. RFC 822 time zones represent the offset from GMT (or UTC) and have the following format:
zone = "UT" / "GMT" ; Universal Time
...
...
/ ( ("+" / "-") 4DIGIT ) ; Local differential
; hours+min. (HHMM)
As you can see, there is no colon between hours and minutes of the time zone. Therefore, the time zone should be +0200.
The most proper solution would be to generate a unicode compliant date string in the first place, but if you are stuck with this format, you may need to preprocess the date string before you pass it to NSDateFormatter.
For example, a quick fix would be using stringByReplacingOccurrencesOfString to get rid of the colon in the time zone:
// dateString --> Fri, 26 Aug 2011 10:51:00 +02:00
dateString = [dateString stringByReplacingOccurrencesOfString:#":"
withString:#""
options:0
range:NSMakeRange(25, [dateString length] - 25)];
// dateString --> Fri, 26 Aug 2011 10:51:00 +0200
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateString];
// date --> 2011-08-26 08:51:00 +0000
No string manipulation required. Change your format string to:
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z:00"];
I tested it and it parses the date you give just fine. When I print the date using [NSDate description], I get 2011-08-26 08:51:00 +0000, which is equivalent to the date given in the string.
Yeah, this is a common problem. A number of servers produce the date with the colon in the timezone, and NSDateFormatter can't (to my knowledge) be convinced to accept it. The solution is to cut out the colon somehow, as suggested.

Objective-C: Unicode Date Format

I am trying to work out how to have the UNICODE representation of
Sun, 03 May 2009 19:58:58 -0700 as eee, dd MMM yyyy HH:mm:s ZZZZ or something. I can't seem to get this working precisely.
Use an NSDateFormatter. It lets you set a particular format string, using the format specifiers from the Unicode spec, then get the formatted date from a given NSDate object using stringFromDate:. Also consider reading Apple's doc about formatting dates. Example:
// Given some NSDate *date
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"eee, dd MMM yyyy HH:mm:ss ZZZZ"];
NSString *formattedDate = [formatter stringFromDate:date];