Unix Timestamp in Dart
Dart's DateTime class uses milliseconds internally — just like JavaScript. Use DateTime.now().millisecondsSinceEpoch for milliseconds, or integer-divide by 1000 for seconds. Flutter and server-side Dart both use the same API.
Code Examples
Current timestamp (milliseconds)
DateTime.now().millisecondsSinceEpoch;
Returns milliseconds since epoch as an int. This is Dart's native epoch unit — analogous to JavaScript's Date.now(). A 13-digit number on current hardware.
Current timestamp (seconds)
DateTime.now().millisecondsSinceEpoch ~/ 1000;
Integer divide by 1000 to get seconds. The ~/ operator is Dart's truncating division — it discards the remainder rather than rounding.
Using microsecondsSinceEpoch (high precision)
DateTime.now().microsecondsSinceEpoch;
Returns microseconds since epoch as an int. Useful for profiling and benchmarking where millisecond precision isn't enough.
Convert timestamp back to DateTime
final dt = DateTime.fromMillisecondsSinceEpoch(ms, isUtc: true);
Reconstruct a DateTime from a millisecond timestamp. Pass isUtc: true to get a UTC DateTime. Omit it and you get a local-timezone DateTime.
Format DateTime as ISO 8601
DateTime.now().toUtc().toIso8601String();
toIso8601String() produces a string like 2024-02-22T00:00:00.000Z. Always call toUtc() first unless you want local time in the output.
Note
Dart's DateTime.millisecondsSinceEpoch returns milliseconds — the same as JavaScript's Date.now(). When integrating Dart/Flutter apps with backend APIs that expect seconds (Python, Go, Unix), remember to divide by 1000. The intl package provides rich formatting if you need locale-aware date strings.
Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.
← Open the converter