I'm programming a birthday app and want to use sqflite to sort the nearest date. Through the page: SQLite query to get the closest datetime I found out about strftime, but unfortunately I don't know how to use it with Flutter. Here are some examples how to sort a date and how I try to find the current date with strftime.
Future<List<Map<String, dynamic>>> getSortDateMapListToDate(bool sort) async {
Database db = await this.database;
String sortingDirection = '$colDate DESC';
if(!sort) {
sortingDirection = '$colDate ASC';
}
DateTime test = DateTime.now();
//var result = await db.query(dateTable, orderBy: sortingDirection);
var result = await db.rawQuery('SELECT * from $dateTable orderBy abs(strftime("%s",$test) - strftime("%s", $colDate))');
return result;
}
Maybe there is another possibility or someone has already worked with rawquerys and strftime.
For any kind of tip or help I would be grateful.
EDIT: With the help from @heitor murara i edit my code:
var result = await db.rawQuery(
"SELECT s.id, s.name, s.date, s.priority, (strftime('%y-%m-%d', '2020-06-30') - strftime('%y-%m-%d', s.$colDate)) as 'TestDate' "
"FROM $dateTable s "
"ORDER BY TestDate"
);
the dates i use: 30.06.2020 and with strftime('%s) i get the rows:
- 2015-08-15 // 32 remaining days
- 2010-11-03 // 126 remaining days
- 2010-07-10 // 10 remaining days
- 1994-07-17 // 17 remaining days
- 1993-06-29 // 364 remaining days
with strftime('%y-%m-%d) i get the rows:
- 1994-07-17 // 17 remaining days
- 1993-06-29 // 364 remaining days
- 2015-08-15 // 32 remaining days
- 2010-11-03 // 126 remaining days
- 2010-07-10 // 10 remaining days
what I am trying to achieve:
- 2010-07-10 // 10 remaining days
- 1994-07-17 // 17 remaining days
- 2015-08-15 // 32 remaining days
- 2010-11-03 // 126 remaining days
- 1993-06-29 // 364 remaining days
a other solution that is in my mind, would be a trigger, who "check" every day if the date changes, the remaining days are calculated dynamically.
Has someone a hint? :)