toDateTime property

DateTime get toDateTime

Converts this number to a DateTime object based on the number of digits.

Example:

print(1609459200000.toDateTime); // Output: 2021-01-01 00:00:00.000 (assuming milliseconds)
print(1609459200000000.toDateTime); // Output: 2021-01-01 00:00:00.000 (assuming microseconds)

Implementation

DateTime get toDateTime {
  if (numberOfDigits == 10) {
    // Assuming seconds and converting to milliseconds
    return DateTime.fromMillisecondsSinceEpoch(toInt() * 1000);
  } else if (numberOfDigits == 13) {
    return DateTime.fromMillisecondsSinceEpoch(toInt()); // Assuming milliseconds
  } else if (numberOfDigits == 16) {
    return DateTime.fromMicrosecondsSinceEpoch(toInt()); // Assuming microseconds
  }
  return DateTime.utc(1970, 1,
      1); // Default to Unix Epoch time if the number of digits does not match any known timestamp format
}