NSDateFormatter returning null value - ios

I'm trying to format a date using NSDateFormatter however for some dates, formatted the same way, it returns the time without the first digit, and for others it returns null.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"EEEE dd/MM/yyyy HH:mm"];
dateFormat.locale = [NSLocale localeWithLocaleIdentifier:#"en_AU"];
NSDate *startDate = [dateFormat dateFromString:startString];
NSDate *endDate = [dateFormat dateFromString:endString];
This date: "Saturday 1/21/2017 17:00" will return (null)
This date: "Thursday 2/9/2017 14:00" will return 2017-09-02 04:00:00 +0000
This date: "Thursday 2/9/2017 20:30" will return 2017-09-02 10:30:00 +0000
Can anyone shed some light on where I am going wrong.
Thanks

EEEE dd/MM/yyyy HH:mm to EEEE MM/dd/yyyy HH:mm. Just a small mistake inverting days and months place.
Because, clearly "Saturday 1/21/2017 17:00", means the 21th of January, because the 21th month in a year doesn't exist (at least, not here).
For the 10h difference, it's due to time zones. In en_AU (east coast I guess), there is a 10h difference from GMT.

Related

iOS Converting strings to date: hour is the same on two different times

I have this code to convert a string to date:
NSString* strToConvert;
NSDate* dtToReturn;
//...code to parse string
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy, HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
dtToReturn = [dateFormatter dateFromString:strToConvert];
I send it this string "08/30/16 02:22:00 PM"
and it returns this date: 2016-08-30 16:22:00 +0000
which is expected since I am +4 from GMT
But if I send it this string: "08/30/16 03:22:00 PM"
it returns the same date: 2016-08-30 16:22:00 +0000
I only pass one string at a time to the method. Am I doing something wrong in my dateformatter?
Because strToConvert uses AM/PM hour time and date formatter is using 24 hour time, this leads to a wrong date conversion. Fix date formatter's dateFormat to use AM/PM hour time: #"MM/dd/yy, hh:mm:ss a"

Using NSDate to convert a string to a date, my dates are changed to 6 months back [duplicate]

This question already has an answer here:
NSDateFormatter dateFromString returns date with wrong month
(1 answer)
Closed 6 years ago.
I have an web service that spits out some dates, and in my iOS app, I'm converting UTC date to local date. I have verified that the web service is spitting out UTC dates and that iOS recognizes it as UTC.
Once i have converted my web service JSON to an NSMutableArray, set a break point and type po [listOfTasks valueForKey:#"LASTEMAILDATE"]
Results
<__NSArrayI 0x157068c80>(
,
7/28/2016 2:01:41 PM,
7/28/2016 2:01:39 PM,
7/28/2016 2:01:42 PM
)
Now, i do a for loop
for (int i = 0; i < listOfTasks.count; i++) {
FireStormCategories *cat = [listOfTasks objectAtIndex:i];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"mm/dd/yyyy hh:mm:ss a"];
NSDate *date = [df dateFromString:cat.LASTEMAILDATE];
MNLog(#"%# was changed to %#", cat.LASTEMAILDATE, [df stringFromDate:date]);
}
that log spits out
was changed to (null)
7/28/2016 2:01:41 PM was changed to 01/28/2016 02:01:41 PM (expecting: 7/28/2016 10:01:41 AM )
7/28/2016 2:01:39 PM was changed to 01/28/2016 02:01:39 PM (expecting: 7/28/2016 10:01:39 AM )
7/28/2016 2:01:42 PM was changed to 01/28/2016 02:01:42 PM (expecting: 7/28/2016 10:01:42 AM )
my first object has no date, the (null) is expected, however, this is where I'm confused. my other 3 dates are changed to a date that reflects 6 months ago and the hours should go back 5 (EST).
You are using wrong format for month. MM is used for month and mm is used for minutes
[df setDateFormat:#"mm/dd/yyyy hh:mm:ss a"];
should be
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
The 01 you are getting is the minutes from the date.

NSDateFormater - returns same year for 2015 and 2016 date [duplicate]

This question already has an answer here:
String formatted date picker date is off
(1 answer)
Closed 7 years ago.
I have found a strange behaviour in NSDateFormatter with LLLL YYYY format, where it returns year 2015 for both 2015-01-01 and 2016-01-01 dates.
Am I missing something or is it a bug in the formatter class?
Code to reproduce:
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = #"LLLL YYYY";
NSDate *d1 = [NSDate dateWithTimeIntervalSince1970:1420070400]; // 2015-01-01T00:00:00Z
NSDate *d2 = [NSDate dateWithTimeIntervalSince1970:1451606400]; // 2016-01-01T00:00:00Z
NSLog(#"%# => %#", d1, [formatter stringFromDate:d1]); // 2015-01-01 00:00:00 +0000 => January 2015
NSLog(#"%# => %#", d2, [formatter stringFromDate:d2]); // 2016-01-01 00:00:00 +0000 => January 2015
Both cases print "January 2015", but I would expect "January 2016" in the second case.
You should use yyyy instead of YYYY, because YYYY is something different...
A deeper explanation from the docs:
A common mistake is to use
YYYY. yyyy specifies the calendar year whereas YYYY specifies the year
(of “Week of Year”), used in the ISO year-week calendar. In most
cases, yyyy and YYYY yield the same number, however they may be
different. Typically you should use the calendar year.
You should use yyyy, not YYYY.

Is it possible to convert input string with any format into date ios?

Is it possible to convert input string with any format into date?
I want to receive ate and time from textfield with any format like date: may 22 2000, wed may 20, may, 30 2000 etc.. and also need to get correct value according to the local timezone.
please help?
NSDateFormatter can help convert any type of date NSString to NSDate. The important thing is to use the right format for the date.
Here is some code you can try
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
You will have to set the date format to whatever format your string is in. Here are some date format specifiers: (Complete list here)
eeee - Local day of week spelled out
yyyy - year (4 digits)
MMMM - Month spelled out
dd - day of month with no leading zeros
HH - hour of day (24 hour format)
mm - minutes of hour (with leading zero)
Edit
The date format for the following dates will be:
May 03 2000 : "MMM dd yyyy"
Mon, Jan 03 : "eee, MMM dd"
Mon 31 Jan : "eee dd MMM"

Bug in dateByAddingTimeInterval

After going almost crazy searching where my code failed ... I was able to isolated this strange behaviour. Look at what hapens when substracting -200 days
NSDate *now = [NSDate date]; //now is 2013-07-19
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"YYYY-MM-dd"];
NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*-199];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-200];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-201];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
newDate1 = [now dateByAddingTimeInterval:60*60*24*-365];
newDateTmp=[format stringFromDate:newDate1];
NSLog(#"now:%# newDateTmp:%#",now,newDateTmp);
logs:
2013-07-19 15:58:46.123 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2013-01-01 // This is OK
2013-07-19 15:58:46.124 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2013-12-31 // This is INCORRECT!!!! Look at the YEAR
2013-07-19 15:58:46.125 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2012-12-30 // This is OK
2013-07-19 15:58:46.127 Vendes[2927:907] now:2013-07-19 13:58:46 +0000 newDateTmp:2012-07-19 // This is OK
I guess it will hapopen also tomorrow substracting 201 .. ?? :)
Any idea?
Thks
PS. I solved it using
NSDate *newDate1= [NSDate dateWithTimeInterval:60*60*daysToAdd sinceDate:now];
that works for any daysToAdd value.
It is not a bug. The format you use is wrong. It should be
#"yyyy-MM-dd"
with lower-cased y
More information on upper-cased Y :
Y - Year (in "Week of Year" based calendars). This year designation
is used in ISO year-week calendar as defined by ISO 8601, but can be
used in non-Gregorian based calendar systems where week date
processing is desired. May not always be the same value as calendar
year.

Resources