How to get a timestamp in Dart?

You almost had it right. You just did not use a named constructor:

void main() {
  print(DateTime.now().millisecondsSinceEpoch);
}

Gives:

1351441456747

See the API documentation for more: https://api.dart.dev/stable/2.10.1/dart-core/DateTime-class.html


This is my solution

DateTime _now = DateTime.now();
print('timestamp: ${_now.hour}:${_now.minute}:${_now.second}.${_now.millisecond}');

Microseconds is also available natively from Dart: (no need to import packages).

void main() {
  print(new DateTime.now().microsecondsSinceEpoch);
}

output:

1591457696860000