Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a timestamp in Dart?

Tags:

epoch

dart

People also ask

How do I display current date and time in Flutter?

dart'; DateFormat dateFormat = DateFormat("yyyy-MM-dd HH:mm:ss"); Here we are going to use DateFormat class of flutter To get the current Time in AM/PM format.


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