subtract function

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

Subtracts given amount (amount) of given units (units) from 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 subtract(
    {@required DateTime orig, @required int amount, @required String units}) {
  switch (units) {
    case "ms":
      orig = orig.subtract(Duration(milliseconds: amount));
      break;
    case "s":
      orig = orig.subtract(Duration(seconds: amount));
      break;
    case "min":
      orig = orig.subtract(Duration(minutes: amount));
      break;
    case "h":
      orig = orig.subtract(Duration(hours: amount));
      break;
    case "d":
      orig = orig.subtract(Duration(days: amount));
      break;
    case "w":
      orig = orig.subtract(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;
}