1

I would like to take TimeOfDay and convert it into a num so that I can subtract another num from it. Once the time and other num have been subtracted from one another I would like to convert the total back to TimeOfDay format.

I currently have no code as I don't know where to begin. Is this possible?

  • Have you looked at the [DateTime class](https://api.dartlang.org/stable/2.4.0/dart-core/DateTime-class.html) ? – Tinus Jackson Jul 01 '19 at 10:35
  • Yes, but I am not sure how to apply it. –  Jul 01 '19 at 11:07
  • Im not quite sure what you want to achieve, You want to subtract Time from one date? – Tinus Jackson Jul 01 '19 at 11:19
  • Have a look at [Subtract](https://api.dartlang.org/stable/2.4.0/dart-core/DateTime/subtract.html) and the [Duration](https://api.dartlang.org/stable/2.4.0/dart-core/Duration-class.html) classes. It might help, use with DateTime class – Tinus Jackson Jul 01 '19 at 11:23
  • I would like to take a starting time, subtract some minutes from the starting time and then have a new time(this time will be earlier) –  Jul 01 '19 at 12:02

1 Answers1

1

Answer to your comments and question.

You can use the standard DateTime class. A few Examples below subtracts several different Duration's.

DateTime now = new DateTime.now();

DateTime oneDayAgo = today.subtract(new Duration(days: 1));
DateTime oneHourAgo = today.subtract(new Duration(hours: 1));
DateTime thirtyMinutesAgo= today.subtract(new Duration(minutes: 30));
DateTime tenSecondsAgo = today.subtract(new Duration(seconds: 10));
DateTime oneDayOneHourAndThirtyMinuesAgo= today.subtract(new Duration(days: 1,hours:1, minutes:30, seconds:10));

Then if you want to format the date

import 'package:intl/intl.dart';

DateTime now = DateTime.now();
String formattedTime = DateFormat.Hms().format(now);
print(formattedTime);

References

  1. DateFormat
  2. DateTime
  3. DateSubtract
  4. Duration

Also see Difference Method as well as if you want to calculate the difference between two dates here is a great answer

Tinus Jackson
  • 3,397
  • 2
  • 25
  • 58