iOS Problem in String to Date conversion

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
My server is sending some JSON Data which contains date like below:

"date":"2012-09-25"
"date":"2012-09-27"
"date":"2012-09-29"
"date":"2012-09-27"
"date":"2012-09-29"

On client side I need to sort these dates. For achieving this I was converting these strings into a date format but getting crash. Below is the code:

Code:
// dates received from the server, see above for eg
NSArray *myDates = .... 

// array to hold date objects
NSMutuableArray *newDates = [[NSMutuableArray alloc] init];

// traverse each string, then convert it into date object then add in newDate
for (NSString *dateStr in myDates)
{
   NSDateFormatter *f = [[NSDateFormatter alloc] init];  // line 1
   [f setDatFormat@"yyyy-mm-d"];                              // line 2  
   NSDate *date = [f dateFromString:dateStr];              // line 3
   [newDates addObject:date];                                   // line 4
}
Problem:
In line 2, the date format yyyy-mm-dd is similar to the format that I am receiving from the server. This is okay, in line 3, it works correct ie dateStr converts in date object and adds in newDates array.

But, if change the format from server like 09-25-2012 ie mm-dd-yyyy then line 3 doesn't convert dateStr into date format, hence date becomes nil and in line 4 it get crashed due to adding nil object.

So how can I solve this issue? I need sorted array before displaying it on the List View.

Additionally I am using following code to sort the dates in ascending array.

Code:
// logic to sort the date in ascending order
NSSortDescriptor *sortDesc = 
            [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES];

[newDates sortUsingDescriptors:[NSArray arrayWithObjects:sortDesc];

for (NSString *date in newDates)
{
      NSLog(@"Date = %@", date];
}
 
Last edited:

ArtOfWarfare

macrumors G3
Nov 26, 2007
8,660
4,200
#1 - I'm noticing one of your formatters has "dd" while the other has just "d".

#2 - Have you checked what string is being passed into the formatter? Try passing a constant NSString in (IE, [f dateFromString:mad:"09-25-2012"]; ) and seeing whether that works. If it doesn't, try having the formatter go from date to string, say with today's date, and see what it outputs. I know some of NSDateFormatter's methods won't simply take the date format as you give it, but will rearrange the elements based on locale... The documentation doesn't say that setDateFormat does so, but it wouldn't be the first time the NSDateFormatter's documentation would be leaving out important details.
 
Comment

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
But, if change the format from server like 09-25-2012 ie mm-dd-yyyy then line 3 doesn't convert dateStr into date format, hence date becomes nil and in line 4 it get crashed due to adding nil object.

So how can I solve this issue? I need sorted array before displaying it on the List View.
You cannot resolve this. You have to send dates in a fixed format from the server side. You actually can resolve between mm-dd-yyyy and yyyy-mm-dd, by counting the characters before the first hyphen, but it is a bad solution. Also, it will fail if your server sends dates in the dd-mm-yyyy format.

Ideally, the server should send UNIX timestamps instead of formatted date strings.
 
Comment

CodeBreaker

macrumors 6502
Nov 5, 2010
494
1
Sea of Tranquility
Ok, in that case can we convert this timestamp in desired date format?
Definitely. You can use the dateWithTimeIntervalSince1970: method of NSDate to get the correct date. After that you can use a date formatter to get the desired string.

Code:
NSDate *theDate = [NSDate dateWithTimeIntervalSince1970:timeIntervalFromServer];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *formattedDateString = [dateFormatter stringFromDate:theDate];
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.