-1

can someone please humbly help me? im fining it extremely confusing.

the dates are like this

1st date ~ 2 May 21:00 2nd date ~ 3 May 04:00

  • If you already have two `DateTime` objects, you can use [`DateTime.difference`](https://api.dart.dev/stable/dart-core/DateTime/difference.html). Also see https://stackoverflow.com/a/69712761/. – jamesdlin May 26 '22 at 03:50

1 Answers1

1

Let's say we have two dates.

var d1 = '2 May 04:00';
var d2 = '3 May 05:00';

First, add this intl package to your project and import it

import 'package:intl/intl.dart';

Next, you need to set a dateformat for your dates above:

var dFormat = DateFormat('d MMM HH:mm');

The date format should be same as your dates orelse you will get an error.

Next, convert the datetime string to your desired format.

var dateTime1 = dFormat.parse(d1, true);
var dateTime2 = dFormat.parse(d2, true);

You can set true if you want UTC time.

Lastly, to get difference between two dates just do the following:

print(dateTime2.difference(dateTime1).inHours); //25

The above print statement will return 25 as the difference between both the given time is 25hours. You can get difference in Minutes too.

Happy Coding!

Md. Kamrul Amin
  • 1,870
  • 1
  • 11
  • 33