xxxxxxxxxx
int daysInMonth(DateTime date) => DateTimeRange(
start: DateTime(date.year, date.month,1),
end: DateTime(date.year, date.month + 1))
.duration
.inDays;
print("Days in current month is ${daysInMonth(DateTime.now())}");
xxxxxxxxxx
DateTime now = new DateTime.now();
DateTime lastDayOfMonth = new DateTime(now.year, now.month + 1, 0);
print("${lastDayOfMonth.month}/${lastDayOfMonth.day}");
xxxxxxxxxx
int year = DateTime.now().year;
int month = DateTime.now().month;
DateTime thisMonth = DateTime(year,month,0);
DateTime nextMonth = DateTime(year,month+1,0);
int days = nextMonth.difference(thisMonth).inDays;
print("Days In This Month: $days");
xxxxxxxxxx
DateTime date = DateTime.now();
int year = date.year.floor();
print(year); //prints only the year excluding the month and day
xxxxxxxxxx
static int getDaysInMonth(int year, int month) {
if (month == DateTime.february) {
final bool isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
return isLeapYear ? 29 : 28;
}
const List<int> daysInMonth = <int>[31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return daysInMonth[month - 1];
}