Home > Mobile >  How to add time and duration in flutter?
How to add time and duration in flutter?

Time:09-06

I have a start time and time duration. Eg: start time is 10:00pm and duration is 120minutes. I want the output like 12:00pm.Add the duration with the time.

CodePudding user response:

If your 10:00pm's DataType is DateTime then use this code.

  DateTime time = DateTime.now(); // Here you can get your current local time
    time.add(Duration(minutes: 120));
    print(time);

CodePudding user response:

You could use the Duration class based on which time measurement unit you'll use.

For example, using minutes:

 DateTime startTime = DateTime.now();
 Duration duration = Duration(minutes: 120);

 DateTime endTime = startTime.add(duration);
 print(endTime);

The output with current DateTime (14:26) will be 2022-09-06 16:26:18.128292.

But you could add seconds instead of minutes, or output just the time and without the day (without using the DateFormat class).

 Duration duration = Duration(seconds: 900);
 DateTime endTime = startTime.add(duration);
 print("${endTime.hour}:${endTime.minute}:${endTime.second}");
  • Related