mercredi 4 mai 2016

Date format needed to work 'order by date' query in iOS

I am storing list of data in sqlite for my iOS app. In DB one column 'addedOn' stores dates from server response in format '30-Jul-2014 07:43:20'. Here I have tried many things on 'addedOn' column but could not fetch date in ASC/DESC order. Some other details are

  1. Column 'addedOn' date type is DATETIME.
  2. Queries tried to fetch in DESC used are:

"SELECT * FROM tableName ORDER BY datetime(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY date(addedOn) DESC LIMIT 1" & "SELECT * FROM tableName ORDER BY addedOn DESC LIMIT 1".

But all these queries failed to give me expected result.

  1. I tried to convert this date in another format while inserting in DB it self but converting of this date format in other date format like 'yyyy-MM-dd HH:mm:ss' isn't working at all. For that tried below code:

    NSString *dateString = @"30-Jul-2014 07:43:20"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:dateString];

    NSString *stringDate = [dateFormatter stringFromDate:dateFromString]; NSLog(@"stringDate %@", stringDate);

So my question is this possible to convert this type format in any other format or is there any thing else that I am missing. Please share your thought and suggest me the changes that I need to do. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire