1

I am taking startdate and endDate as input from user with DateTime as datatype in Flutter(Dart). These fields will be stored in Firestore may be as timestamp format. Now we need to display the difference of endDate and startDate on client side which can be a live timer in format as "13 Hrs 45 mins" then after some mins it should be "13 Hrs 42 mins".

May I know how can we achieve this using Firestore or may be cloud functions for Flutter apps? Thanks

dshukertjr
  • 15,244
  • 11
  • 57
  • 94
Akshay
  • 23
  • 5

1 Answers1

1

In flutter, Timestamp can be easily converted to DateTime, and DateTime has convenient method called difference that will give you difference between two DateTime objects as Duration object.

Here is a simple example:

final Timestamp endTime = Timestamp(1000, 0);
final Timestamp startTime = Timestamp(500, 0);
final DateTime endTimeDate = endTime.toDate();
final DateTime startTimeDate = startTime.toDate();
final Duration difference = endTimeDate.difference(startTimeDate);
dshukertjr
  • 15,244
  • 11
  • 57
  • 94
  • Thanks for this. I am taking input in DateTime format as follows :- selectedStartDate = 2021-01-03 10:41:00.000 selectedEndDate = 2021-01-05 16:41:00.000 And I am getting difference duration as : 54:00:00.000000 So my preferable output should be as 54 Hrs 00 mins. Can you please help here how to do this? – Akshay Jan 03 '21 at 05:15
  • @Akshay You can do something like this: https://stackoverflow.com/questions/54775097/formatting-a-duration-like-hhmmss – dshukertjr Jan 03 '21 at 05:23