DateFormatter keep returning null - objective-c

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

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.

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

NSDateFormatter return incorrect date from string

I have a method,
+ (NSDate *) convertToDateFrom:(NSString *) dateString
{
if (dateString == nil || [dateString isEqual:#""]) return nil; //return nil if dateString is empty
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE, dd MMMM yyyy HH:mm"];
NSDate *date = [df dateFromString:dateString];
return date;
}
When I pass,
#"Monday, 21 November 2011 17:01" //Passed string
It returns a wrong date,
2011-11-21 23:14:00 +0000 // Output
I am not sure whether I am using those flags correctly or NSDateFormatter isn't properly converting my string to date.
Am I doing something wrong?
Thanks
The +0000 at the end of the date indicates GMT. All dates are stored relative to GMT; when you convert a date to a string or vice versa using a date formatter, the offset to your time zone is included. You can use NSDateFormatter's -setTimeZone: method to set the time zone used.
In short, you're not doing anything wrong in your code. Use [df stringFromDate:date]; to see that the date is correct. (You can also use NSDate's -descriptionWithCalendarFormat:timeZone:locale:.)
try using
df stringFromDate:date
Following worked on mine,
NSLog(#"Date for locale %#: %#",
[[dateFormatter locale] localeIdentifier], [df stringFromDate:date]);
gave me output as :
Date for locale en_US: Wednesday, 26 June 2013 15:50
Try setting the time zone and locale.
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];

iOS dateFormatter dateFromString month issue

Hopefully this is just a quick one and I'm missing something simple...
I've got an NSDateFormatter, which I'm using to convert the string 2011-11-10 into a Date object.
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:#"YYYY-MM-DD"];
// input of 2011-11-10, output of 2011-01-10 00:00:00 +0000
[appointment setDate:[fmtDate dateFromString:
[tempAppointment objectForKey:#"date"]
]];
The return from the NSDateFormatter is stored in a managedObjectContext. My problem is that the dateFormatter is returning the date as 2011-01-10 00:00:00 +0000
Why is it reducing the month from Nobvember to January? It's retaining the year and the day fine, but not the month.
Do I need to include the hours when I store the date? Or is it something to do with the format I set?
Try the format #"yyyy-MM-dd" for the format as specified here in the "Use Format Strings to Specify Custom Formats" section. It has an example and list YYYY as a common mistake.
Your date format string is not correct using YYYY. Try yyyy-M-d or yyyy-MM-dd
NSString *dateString = #"2011-11-10";
NSDateFormatter *fmtDate = [[NSDateFormatter alloc] init];
[fmtDate setDateFormat:#"yyyy-M-d"];
NSDate *date = [fmtDate dateFromString:dateString];
NSLog(#"date: %#", date);
Outputs:
2011-11-10 20:01:40.638 Craplet[81514:707] date: 2011-11-10 05:00:00 +0000
See:
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

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.