add function

DateTime add (
  1. {@required DateTime orig,
  2. @required int amount,
  3. @required String units}
)

Adds given amount (amount) of given units (units) to given DateTime (orig).

Returns the modified DateTime.

Accepted units are:

  • 'ms' (milliseconds)

  • 's' (seconds)

  • 'min' (minutes)

  • 'h' (hours)

  • 'd' (days)

  • 'w' (weeks)

  • 'mon' (months)

  • 'y' (years)

Implementation

DateTime add({@required DateTime orig, @required int amount, @required String units}) {
  switch (units) {
    case "ms":
      orig = orig.add(Duration(milliseconds: amount));
      break;
    case "s":
      orig = orig.add(Duration(seconds: amount));
      break;
    case "min":
      orig = orig.add(Duration(minutes: amount));
      break;
    case "h":
      orig = orig.add(Duration(hours: amount));
      break;
    case "d":
      orig = orig.add(Duration(days: amount));
      break;
    case "w":
      orig = orig.add(Duration(days: amount * 7));
      break;
    case "mon":
      orig = _addMonths(orig, amount);
      break;
    case "y":
      orig = _addMonths(orig, amount * 12);
      break;
    default:
      throw ArgumentError("This unit is unsupported. See docs for supported units...");
  }
  return orig;
}