toDateTime method

DateTime toDateTime({
  1. TimestampUnit timestampUnit = TimestampUnit.milliseconds,
  2. NumberRoundType roundType = NumberRoundType.round,
})

Converts double to DateTime

In some languages, the timestamp takes the integer part as the second and the decimal part as the millisecond or other. This method should be used.

Such as:

  • 1234567890.123456.toDateTime(timestampUnit: TimestampUnit.seconds) => DateTime(2009, 2, 14, 7, 31, 30, 123, 456)
  • 1234567890123.456.toDateTime() => DateTime(2009, 2, 14, 7, 31, 30, 123, 456)

Implementation

DateTime toDateTime({
  TimestampUnit timestampUnit = TimestampUnit.milliseconds,
  NumberRoundType roundType = NumberRoundType.round,
}) {
  var v = this;

  if (timestampUnit == TimestampUnit.seconds) {
    v = v * 1000 * 1000;
  } else if (timestampUnit == TimestampUnit.milliseconds) {
    v = v * 1000;
  }

  final time = v.roundToInt(roundType: roundType);

  return DateTime.fromMicrosecondsSinceEpoch(time);
}