Custom date string to NSDate - objective-c

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

Related

Difference between dateFromString and stringFromDate for this pattern 'YYYY'

want to understand the Difference between the Difference between dateFromString and stringFromDate for this pattern "YYYY".
Because I have written the below logics for dateFromString and stringFromDate but result is different like Mon Dec 30 09:05:00 2019 and 2020/12/30 09:05
// convert string to date
NSString *dateStr = #"2019-12-30 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm"];
//NSDate *date = [dateFormat dateFromString:dateStr];
NSDate *dateValue = [dateFormat dateFromString:dateStr];
NSLog(#"string to date == %#",dateValue);
// convert date to string
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[df1 setLocale:locale];
[df1 setDateFormat:#"YYYY/MM/dd HH:mm"];
NSString *datestr = [df1 stringFromDate:dateValue];
NSLog(#"date to string == %#", datestr);
so out put is below
string to date == Mon Dec 30 09:05:00 2019
date to string == 2020/12/30 09:05
Both or same pattern YYYY It is working as a week based calander. but why year value 2019 and 2020 is differing when using following methods dateFromString and stringFromDate?
When you use yyyy, you will get the calendar numeric year.
The YYYY you will get the week of year, that can do this boundary rollover that you are experiencing. Read the docs at: http://www.unicode.org/reports/tr35/tr35-dates.html#dfst-year
Always when I have problems with date formatters I usually go to this website https://nsdateformatter.com/.
The week number of 2019-12-30 is week 1 of 2020. The year of week 1 of 2020 is 2020. Try format yyyy/MM/dd HH:mm YYYY w (week of year) the result is 2019/12/30 09:05 2020 1.

DateFormatter keep returning null

I have a date i take from twitter :
NSString *date=[twt objectForKey:#"created_at"];
NSLog(#"%#",date);
NSDateFormatter *df = [ [NSDateFormatter alloc] init] ;
[df setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate *newdate = [df dateFromString:date];
NSLog(#"new: %#",newdate); //null
the date is : Thu Jun 12 20:56:53 +0000 2014 and i get null on the new date .
What am i missing ?
the date formatter for that format (Thu Jun 12 20:56:53 +0000 2014) would be this:
"EEE MMM d HH:mm:ss Z yyyy"
rather than this "dd-MM-yyyy HH:mm:ss".
The date formatting string you've shared sure does't look like it matches the date you're getting from Twitter. The docs for dateFromString state...
A date representation of string interpreted using the receiver’s
current settings. If dateFromString: can not parse the string, returns
nil.
Read up!
http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

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.

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.