I just did it for the date.
You can copy the implementation made in C# in dart.
void main() {
final dateTime = DateTime.now();
print(DateTimeUtils.dateToTicks(
dateTime.year,
dateTime.month,
dateTime.day,
));
}
class DateTimeUtils {
static const int TicksPerMillisecond = 10000;
static const int TicksPerSecond = TicksPerMillisecond * 1000;
static const int TicksPerMinute = TicksPerSecond * 60;
static const int TicksPerHour = TicksPerMinute * 60;
static const int TicksPerDay = TicksPerHour * 24;
static List<int> daysToMonth365 = [
0,
31,
59,
90,
120,
151,
181,
212,
243,
273,
304,
334,
365,
];
static List<int> daysToMonth366 = [
0,
31,
60,
91,
121,
152,
182,
213,
244,
274,
305,
335,
366,
];
static int dateToTicks(int year, int month, int day) {
if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) {
List<int> days = isLeapYear(year) ? daysToMonth366 : daysToMonth365;
if (day >= 1 && day <= days[month] - days[month - 1]) {
int y = year - 1;
double n = y * 365 + y / 4 - y / 100 + y / 400 + days[month - 1] + day - 1;
return n * TicksPerDay as int;
}
}
throw new ArgumentError();
}
static bool isLeapYear(int year) {
if (year < 1 || year > 9999) {
throw new ArgumentError();
}
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
}
2
solved Convert date time to C# timetick in Dart (Flutter) [closed]