toDateTimeN static method
Converts a dynamic value to a nullable DateTime.
d
- The dynamic value to be converted.
unit
- The time unit of the input value (default is seconds).
Returns a DateTime if the conversion is successful, otherwise null.
Implementation
static DateTime? toDateTimeN(
dynamic d, {
TimeUnit unit = TimeUnit.seconds,
}) {
if (d is int) {
return DateTime.fromMillisecondsSinceEpoch(
unit == TimeUnit.seconds ? d * 1000 : d,
);
}
if (d is double) {
return DateTime.fromMillisecondsSinceEpoch(
unit == TimeUnit.seconds ? d.toInt() * 1000 : d.toInt(),
);
}
if (d is String) {
return DateTime.tryParse(d);
}
return null;
}