Problem in String to Date conversion

Discussion in 'iOS Programming' started by Narendar Singh, Sep 30, 2012.

  1. Narendar Singh, Sep 30, 2012
    Last edited: Oct 1, 2012

    Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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];
    }
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    #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.
     
  3. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #3
    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.
     
  4. Narendar Singh thread starter macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #4
    Ok, in that case can we convert this timestamp in desired date format?
     
  5. CodeBreaker macrumors 6502

    Joined:
    Nov 5, 2010
    Location:
    Sea of Tranquility
    #5
    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];
    
     

Share This Page