Retrieving the records from the sqlite database on the basis of date parameter is little hard. Because versatile requisition might require getting information in the underneath conduct:
-
Fetch number of records request by date climbing and dropping
-
Fetch records between two dates
-
Fetch record after a date
-
Fetch record before a date
In sqlite database we store date as string and all the date capacities neglects to recover information consistent with the date. Saving date string in an organized manner can take care of the date issue.
To tackle this issue dependably store date in the arrangement 'yyyy-MM-dd Hh:mm:ss' OR 'yyyy-MM-dd Hh:mm:ss a'. Here the single digit for date, month, hour, moment, and seconds might as well accompany the 0.
For Example:
Assuming that our date is 'Jun 5th, 2013 6:30:11' we can change this date to a arrangement of 'yyyy-MM-dd Hh:mm:ss' OR 'yyyy-MM-dd Hh:mm:ss a'.
In the wake of changing the date to this organization, the date will be:
'2013-06-05 06:30:11' OR '2013-06-05 06:30:11 AM'
In this date group we can sort the sqlite information by utilizing the sqlite query. We can bring the records after or before a date. We can bring the information between two dates.
Select * from tablename request by date sac
Select * from tablename request by date desc
Select * from tablename where date > '2014-01-01 02:30:18'
Select * from tablename where date between '2013-09-01 02:30:18' and '2014-01-01 02:30:18'
Besides, here you need to change the date arrangement to show in your views. Doesn’t stress over this simply make a method which will furnish a proportional payback date organized string.
Use this date format in your application globally.
#define databasedateformat @"yyyy-MM-dd Hh:mm:ss".
Date configuration changing strategy will take three parameters in it.
1. Current Date String (strdate)
2. Current Date Format (strcurrentformat OR databasedateformat)
3. Specified Date Format (strspecifieddateformat)
For Example:
- (Nsstring *)getformatteddate:(nsstring *)strdate currentdateformat:(nsstring *)strcurrentformat withdate:(nsstring *)strspecifieddateformat
{
Nsstring *resulteddatestring;
Nsdateformatter *dateformatter = [[nsdateformatter alloc]init];
[dateformatter setdateformat:strcurrentformat];
Nsdate *currentdate = [dateformatter datefromstring:strdate];
if(currentdate !=nil)
{
[dateformatter setdateformat:strspecifieddateformat];
resulteddatestring = [dateformatter stringfromdate:currentdate];
}
else
{
resulteddatestring = strdate;
}
return resulteddatestring;
}
Above approach will undertake the issue acknowledged with date arrangement.
|