jogboms/time.dart

[Feature Request] New methods on DateTime (firstDayOfMonth, lastDayOfMonth, lastDayOfWeek, firstDayOfWeek, firstDayOfYear, lastDayOfYear)

FMorschel opened this issue · 0 comments

Looking for lastDayOfMonth like feature, found this StackOverflow question and I really liked the way Yogesh Parwani mixed Chris Buckett and Kai Sellgren awnsers. As pointed by Juniper Belmont on Buckett's answer, I do think this could be a new feature as mnordine suggested here.

(Better code from here - lrhn)

Code bellow:

extension DateTimeFirstLast on DateTime {
  DateTime get firstDayOfWeek => DateTime.utc(year, month, day + 1 - weekday);
  DateTime get lastDayOfWeek => DateTime.utc(year, month, day + 7 - weekday);  
  DateTime get firstDayOfMonth => DateTime.utc(year, month, 1);
  DateTime get lastDayOfMonth => DateTime.utc(year, month + 1, 0);
  DateTime get firstDayOfYear => DateTime.utc(year, 1, 1);
  DateTime get lastDayOfYear => DateTime.utc(year, 12, 31);
}