'mysql timestamp calculations in dart

I am getting 2 timestamps in my Flutter/Dart app via an API in the following format:

2022-04-05 10:00:00 / 2022-04-05 12:00:00

I would like to calculate the time difference of the two and round it to one decimal place.

How do I do this using dart?



Solution 1:[1]

You can try something like this with the intl package.

import 'package:intl/intl.dart';

void main() {
 
  DateFormat formatter =  DateFormat("y-M-d H:m:s");
  var date_a = formatter.parse("2022-04-05 10:00:00");
  var date_b = formatter.parse("2022-04-05 12:00:00");
 
  print(date_b.difference(date_a));

}

Reference: https://pub.dev/documentation/intl/latest/intl/intl-library.html

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 Shri Hari L