简体   繁体   中英

How to find difference between two times in 24 hour format - Flutter?

I have to find the difference between two times in 24 hour format. I have the two time strings, Eg: 10:40 and 18:20. How can I find the difference between these two times in Flutter?

You can use intl package.

var format = DateFormat("HH:mm");
var one = format.parse("10:40");
var two = format.parse("18:20");
print("${two.difference(one)}"); // prints 7:40

A complete answer for perfect calculation is given bellow


    String start_time = formateTime('12:00'); // or if '24:00'
    String end_time = formateTime('24:00'); // or if '12:00

    var format = DateFormat("HH:mm");
    var start = format.parse(start_time);
    var end = format.parse(end_time);


    if (start.isAfter(end)) {
      print('start is big');
      print('difference = ${start.difference(end)}');
    } else if(start.isBefore(end)){
      print('end is big');
      print('difference = ${end.difference(start)}');
    }else{
      print('difference = ${end.difference(start)}');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM