'How to compare date with timestamp field type in firebase firestore database flutter?
I want to get the records matching the date. However, I'm unable to do so because the field type in firebase is timestamp. I only want the records exactly matching that specified date. Please let me know if this is possible. Thank you.
DateTime tsdate = DateTime.fromMillisecondsSinceEpoch(mydate*1000);
print(DateTime(tsdate.year, tsdate.month, tsdate.day));
//the date is 2022-04-06 00:00:00.000
FirebaseFirestore.instance
.collection('cars')
.where('datecreated',
isEqualTo: DateTime(tsdate.year, tsdate.month, tsdate.day)),
Solution 1:[1]
If you want to query items, that have been created at a specific date, try this:
final startTime = DateTime(2022,4,5);
final endTime = DateTime(2022,4,6);
FirebaseForestore.instance.
.collection('cars')
.where('datecreated', isGreaterOrEqualTo: startTime)
.where('datecreated', isLessOrEqualTo : endTime)
If you wanted to use the isEqualTo operator you'd need to specify the millisecond that you want to query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Christian |
