#StackBounty: #flutter #dart Flutter how to subtract time from other time

Bounty: 250

I have 2 times which I need to do subtract and I am almost close but there is one big issue

I have 2 times in string-like 10:00AM and 10:00PM

And my code is this

   var df =  DateFormat("hh:mm");                                           
   var durationStart =  DateFormat('HH:mm').format(df.parse(10:00AM));
   var durationEnd =  DateFormat('HH:mm').format(df.parse(10:00PM));
   print('durationStart ${durationStart}');
   print('durationEnd ${durationEnd}');

   var Startparts = durationStart.split(':');
   var startDurationSet = Duration(hours: int.parse(Startparts[0].trim()), minutes: int.parse(Startparts[1].trim()));

   var Endparts = durationEnd.split(':');
   var endDurationSet = Duration(hours: int.parse(Endparts[0].trim()), minutes: int.parse(Endparts[1].trim()));

   print('startDurationSet ${startDurationSet}');

   var result = Duration(hours: int.parse(Endparts[0].trim()) - int.parse(Startparts[0].trim()) , minutes: int.parse(Startparts[1].trim()) - int.parse(Endparts[1].trim()));
       print('result ${result.toString().replaceAll('-', '')}');

So I have 2 times one is startTime and one is End time. I simply need a difference between hours. for example, I have 10:00Am and 01:00PM i need 3hours but it’s showing 9hours. But what I am receiving is if I have 10:00AM and 10:00pm it’s showing 0 hours but its needs to show 12. Same

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.