uaestats_fe/lib/infrastructure/services/packages/date_helper.dart
2026-05-16 11:31:32 +05:30

46 lines
1.2 KiB
Dart
Executable File

abstract class DateHelper {
static int? monthIndexFromString(String month) {
const monthsMap = {
'january': 1,
'february': 2,
'march': 3,
'april': 4,
'may': 5,
'june': 6,
'july': 7,
'august': 8,
'september': 9,
'october': 10,
'november': 11,
'december': 12,
};
return monthsMap[month.trim().toLowerCase()];
}
static double convertForXIndexOfYears(DateTime dt) {
final year = dt.year;
final monthFraction = (dt.month - 1) / 12;
final dayFraction = dt.day / 365;
return year + monthFraction + dayFraction;
}
static int getDaysInMonth(int month, int year) => switch (month) {
// January, March, May, July, August, October, December
1 || 3 || 5 || 7 || 8 || 10 || 12 => 31,
// April, June, September, November
4 || 6 || 9 || 11 => 30,
// February
2 => isLeapYear(year) ? 29 : 28,
int() => throw Exception(
'Invalid month number. Month number should be between 1 and 12.',
),
};
static bool isLeapYear(int year) {
if (year % 4 != 0) return false;
if (year % 100 != 0) return true;
if (year % 400 == 0) return true;
return false;
}
}