Home > Net >  how to find difference btw 21:00 pm - 1:00 am in flutter
how to find difference btw 21:00 pm - 1:00 am in flutter

Time:05-27

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

CodePudding user response:

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!

  • Related