tt_DATESTART function

dynamic tt_DATESTART(
  1. dynamic v1,
  2. dynamic v2
)

tt_DATESTART(date, unit): start of a period; unit='M'=start of month/'Y'=start of year/'W'=Monday

Implementation

dynamic tt_DATESTART(dynamic v1, dynamic v2) {
  final d = _vToDateTime(v1);
  if (d == null) return null;
  var u = varToStr(v2).trim().toUpperCase();
  if (u.isEmpty) u = 'M';
  switch (u[0]) {
    case 'M': return DateTime(d.year, d.month, 1);
    case 'Y': return DateTime(d.year, 1, 1);
    case 'W':
      // go back to Monday of this week
      final diff = d.weekday - 1; // Mon=1
      return d.subtract(Duration(days: diff));
    default: return d;
  }
}