07-21-2023, 10:00 PM
There are various ways to convert `TimeStamp to DateTime` which differs based on the scenario.
I have tried to gather all the possible ways to convert `Firebase timeStamp` to `DateTime`. Pick which works for you.
---
#### Conversion of `Firebase timestamp` to `DateTime`:
1. document['timeStamp'].toDate()
2. (document["timeStamp"] as Timestamp).toDate()
3. DateTime.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch);
5. Timestamp.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch).toDate();
5. If `timeStamp` is in `microseconds` use:
DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);
6. If `timeStamp` is in `milliseconds` use:
DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
7. Add the following function in your dart file.
String formatTimestamp(Timestamp timestamp) {
var format = new DateFormat('yyyy-MM-dd'); // <- use skeleton here
return format.format(timestamp.toDate());
}
call it as `formatTimestamp(document['timestamp'])`
I have tried to gather all the possible ways to convert `Firebase timeStamp` to `DateTime`. Pick which works for you.
---
#### Conversion of `Firebase timestamp` to `DateTime`:
1. document['timeStamp'].toDate()
2. (document["timeStamp"] as Timestamp).toDate()
3. DateTime.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch);
5. Timestamp.fromMillisecondsSinceEpoch(document['timeStamp'].millisecondsSinceEpoch).toDate();
5. If `timeStamp` is in `microseconds` use:
DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000000);
6. If `timeStamp` is in `milliseconds` use:
DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
7. Add the following function in your dart file.
String formatTimestamp(Timestamp timestamp) {
var format = new DateFormat('yyyy-MM-dd'); // <- use skeleton here
return format.format(timestamp.toDate());
}
call it as `formatTimestamp(document['timestamp'])`