Getting date out of array with NSDateFormatter - objective-c

I am reading in a file of strings. Each string looks something like:
!AIVDO,1,1,,B,11b4N?HPs0KLrBDIStg;q?w22<06,0*5A,21/10/2011 12:01:13 PM
What I have done so far is:
NSArray *arrayFromString = [theString componentsSeparatedByString:#","];
NSString *dateString = [tempArrayFromAISString objectAtIndex:7];
// dateString = #"21/10/2011 12:01:13 PM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
This returns an NSDate of (null).
The string is set ok because when I output it with NSLog it looks good.
The date formatter is ok because if I uncomment the line commented out and set the date string myself it works.
Any ideas as to why the string from the array does not work?
Thanks in advance for any help.

Change to
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm:ss a"];

Your example contains a typo. arrayFromString≠ tempArrayFromAISString
This works for me
NSString *theString = #"!AIVDO,1,1,,B,11b4N?HPs0KLrBDIStg;q?w22<06,0*5A,21/10/2011 12:01:13 PM";
NSArray *arrayFromString = [theString componentsSeparatedByString:#","];
NSString *dateString = [arrayFromString objectAtIndex:7];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"%#", date);

There was a newline character at the end because I was reading from a file. I fixed it with:
dateString = [dateString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
This gets rid of the end of line character.

Related

Date format changing issue Objective C

All,
I want date format in dd/MMM/yyyy format below is the code i am using.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:#"dd/MMM/yyyy"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
But in the dateString i am getting in Oct 21,2014 which is diffrent how can i get in "21/Oct/2014" format please let me know
The issue is that you're calling setDateStyle: instead of setDateFormat:
Use this code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MMM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSLog(#"%#", dateString);
This code outputs "22/Oct/2014"

How to convert Date from JSON to NSDate

Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.

Convert NSString in 12 hour time format

In my project I have an NSString that contains data like this
NSString *s=#"20";
I want to convert this as "08:00 ". How can I do this?
I tried this one:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString* dateString = [dateFormatter stringFromDate:#"20"];
but I am getting this dateString as null value.
You input as NSString, need output as NSString.
So intermediate work of NSDate and NSDateFormatter are not required.
You can attain this by :
NSString *s=#"20";
NSString *time=[NSString stringWithFormat:#"%d:00",[s integerValue]%12];
Please try to use this one .I hope it will work fine...
NSString *s=#"20";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"HH"];
NSDate *date = [df dateFromString:s];
[df setDateFormat:#"hh:mm a"];
NSString *newTime = [df stringFromDate:date];
NSLog(#"Time : %#",newTime);

NSDateFormatter and /'s

I have a NSString that I want to load into a NSDate. The string looks like: 21/10/2011 5:42:02 PM
What I have written is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateString];
The date ends up with a null value.
There is obviously something wrong with the date formatter but I am not sure what.
Any help would be greatly appreciated.
If the string doesn't match the format, then it will return null.
For example, here's an example that matches the format and one that doesn't:
NSString *dateString = #"11/09/2011 12:22:02";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"d/M/yyyy h:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
NSLog(#"dateString; %#", date);
NSString *dateString2 = #"2011-11-09 12:22:02 +0000";
NSDate *date2 = [dateFormat2 dateFromString:dateString2];
NSLog(#"dateString; %#", date2);
This outputs:
2011-11-09 07:27:54.342 Craplet[45608:707] dateString; 2011-09-11 05:22:02 +0000
2011-11-09 07:27:54.343 Craplet[45608:707] dateString; (null)

Why does my NSDateFormatter return nil when parsing?

my code is like this
NSString *tempDate = [NSString stringWithString:tempReviewData.pubDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"HH:mm a"];
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
My newDate is getting nil at this point i dont know why
It seems to work for me but it depends on the format of tempReviewData.pubDate.
When I use invalid format, like #"6:30 M", I get null as well.
This is working:
NSString *tempDate = [NSString stringWithString:#"6:30 PM"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"HH:mm a"];
NSDate * newDate = [dateFormatter dateFromString:tempDate];
NSString * str = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"date: %#", newDate);
NSLog(#"str: %#", str);
Output:
2010-03-08 22:36:57.904 x[4340:903] date: 1970-01-01 12:30:00 +1000
2010-03-08 22:36:57.905 x[4340:903] str: 22:36 PM
NSDate *newDate = [dateFormatter dateFromString:tempReviewData.pubDate];
Does pubDate return an NSString, or an NSDate?
If it returns a string, then you should rename that property to clearly indicate that.
If it returns a date (NSDate), then trying to parse it as a string will not work, since it is not a string; moreover, you can cut out all this formatter code, since you already have the date object you're after.
It seems the NSDateFormatter has gotten very picky.
-(void)dateFormatterTests {
NSDateFormatter *formatter;
formatter = [[NSDateFormatter alloc] init];
#ifdef WORKS
[formatter setDateFormat:#"yyyy-MM-dd"];
#elif defined(ALSO_WORKS)
[formatter setDateFormat:#"yyyy MM dd"];
[formatter setLenient:YES];
#else // DOESN'T WORK
[formatter setDateFormat:#"yyyy MM dd"];
#endif
// Works per comments above
NSLog(#"dFS: %#", [formatter dateFromString:#"2010-01-13"]);
// Never works with any of the above formats
NSLog(#"dFS: %#", [formatter dateFromString:#"2010-01-13 22:00"]);
[formatter release]; formatter = nil;
}