subtract method
Subtract val
by unit
. Supports shorthand.
Example:
subtract(1, 'date');
subtract(1, 'd');
Implementation
dynamic subtract(int val, String unit) {
final processedUnit = Unit.fromShorthand(unit);
final duration = u.durationFromUnit(val, processedUnit);
if (duration != null) {
final d = clone();
d._time = d._time.subtract(duration);
d._parseTime();
return d;
} else {
if (unit == Unit.y || unit == 'y') {
return _cloneAndSetSingleValue(Unit.y, year() - val);
} else if (unit == Unit.m || unit == 'M') {
final int result = month() - val;
final d = clone();
if (result > 0) {
d.setValue(Unit.m, result);
} else {
d
..setValue(Unit.y, d.year() - (result.abs() ~/ 12 + 1))
..setValue(Unit.m, 12 - result.abs() % 12);
}
d.finished();
return d;
}
}
return null;
}