How to Round up the Date-Time nearest to 30 min interval in DART (Flutter)?

2,116

Solution 1

You can use this function to roundup the time.

DateTime alignDateTime(DateTime dt, Duration alignment,
    [bool roundUp = false]) {
  assert(alignment >= Duration.zero);
  if (alignment == Duration.zero) return dt;
  final correction = Duration(
      days: 0,
      hours: alignment.inDays > 0
          ? dt.hour
          : alignment.inHours > 0
              ? dt.hour % alignment.inHours
              : 0,
      minutes: alignment.inHours > 0
          ? dt.minute
          : alignment.inMinutes > 0
              ? dt.minute % alignment.inMinutes
              : 0,
      seconds: alignment.inMinutes > 0
          ? dt.second
          : alignment.inSeconds > 0
              ? dt.second % alignment.inSeconds
              : 0,
      milliseconds: alignment.inSeconds > 0
          ? dt.millisecond
          : alignment.inMilliseconds > 0
              ? dt.millisecond % alignment.inMilliseconds
              : 0,
      microseconds: alignment.inMilliseconds > 0 ? dt.microsecond : 0);
  if (correction == Duration.zero) return dt;
  final corrected = dt.subtract(correction);
  final result = roundUp ? corrected.add(alignment) : corrected;
  return result;
}

and then use it the following way

void main() {
  DateTime dt = DateTime.now();
  var newDate = alignDateTime(dt,Duration(minutes:30));
  print(dt);  // prints 2022-01-07 15:35:56.288
  print(newDate); // prints 2022-01-07 15:30:00.000
}

Solution 2

extension DateTimeExt on DateTime {
  DateTime get roundMin =>
      DateTime(this.year, this.month, this.day, this.hour, () {
        if (this.minute <= 15) {
          return 0;
        } else if (this.minute > 15 && this.minute <= 45) {
          return 30;
        } else {
          return 60;
        }
      }());
}

You can do It with a simple extension just call it like this

var a = DateTime(2021, 5, 4, 3, 46, 4, 7);
print(a.roundMin);

Solution 3

This function converts a DateTime to the nearest 30 minute mark in a clock. Be warned that this 30 minute mark is obtained with respect to the local time zone of the machine in which this code runs on.

 DateTime roundWithin30Minutes(DateTime d) {
  final int deltaMinute;
  if (d.minute < 15) {
    deltaMinute = -d.minute; // go back to zero
  } else if (d.minute < 45) {
    deltaMinute = 30 - d.minute; // go to 30 minutes
  } else {
    deltaMinute = 60 - d.minute;
  }
  return d.add(Duration(
      milliseconds: -d.millisecond,
      // reset milliseconds to zero
      microseconds: -d.microsecond,
      // reset microseconds to zero,
      seconds: -d.second,
      // reset seconds to zero
      minutes: deltaMinute));
}

If you are presenting this DateTime in another non local time zone whose offset duration is not a multiple of 30 minutes (eg: Nepal time zone is GMT+5:45) this implementation will not work.

Share:
2,116
Niraj
Author by

Niraj

Love Programming.

Updated on December 18, 2022

Comments

  • Niraj
    Niraj over 1 year

    I would like to round DateTime to the nearest 30 mins. Is there rounding mechanism provided in DART?