Dart/Flutter - Convert Epoch Timestamp to DateTime & Vice Versa

This tutorial shows you how to convert between Unix epoch timestamp and Dart's DateTime.

In computer systems, it's very common to use the Unix epoch as a time representation. Unix epoch is the amount of elapsed time since 1 January 1970 at 00:00:00 UTC. In Dart, an instant of time is usually stored as DateTime. In this tutorial, I am going to show how to convert an epoch timestamp to a DateTime and the other way around.

Convert Epoch to DateTime

If you have an epoch timestamp value, you can easily convert it to a DateTime object. Dart's DateTime has two methods fromMillisecondsSinceEpoch and fromMicrosecondsSinceEpoch. As the names suggest, those methods are used to convert from an epoch timestamp value, in milliseconds and microseconds units respectively.

Below are the examples using fromMillisecondsSinceEpoch. If the value is 0, it represents 1970-01-01 00:00:00 at UTC time. The default time zone depends on the local system that runs the code. My time zone is GMT+7 and that's why the outputs are in GMT+7.

The second example uses a positive value. As you can expect, the output is a date time which occurs after the epoch time. If you pass a negative value, the resulting date time occurred before the epoch time.

  final dt1 = DateTime.fromMillisecondsSinceEpoch(0);
  print(dt1); // 1970-01-01 07:00:00.000

  final dt2 = DateTime.fromMillisecondsSinceEpoch(1685411325000);
  print(dt2); // 2023-05-30 08:48:45.000

  final dt3 = DateTime.fromMillisecondsSinceEpoch(-1685411325000);
  print(dt3); // 1916-08-05 04:53:19.000

Below is another example using fromMicrosecondsSinceEpoch. It's actually the same value as the second example above, but represented in microseconds.

  final dt2b = DateTime.fromMicrosecondsSinceEpoch(1685411325000000);
print(dt2b); // 2023-05-30 08:48:45.000

If you want to get the result in UTC, add isUtc argument and set the value to true.

  final dt2Utc = DateTime.fromMillisecondsSinceEpoch(1685411325000, isUtc: true);
  print(dt2Utc); // 2023-05-30 01:48:45.000Z
  
  final dt2bUtc = DateTime.fromMicrosecondsSinceEpoch(1685411325000000, isUtc: true);
  print(dt2bUtc); // 2023-05-30 01:48:45.000Z

Convert DateTime to Epoch

If you have a DateTime instance, getting the time since the Unix epoch is very simple. Just access the millisecondsSinceEpoch or microsecondsSinceEpoch property to get the value in milliseconds or microseconds respectively. The value is at most 8,640,000,000,000,000ms or 8,640,000,000,000,000,000us (100,000,000 days) from the Unix epoch. The output will be negative if the time is before the epoch time.

  final dt1 = DateTime(1970, 1, 1, 7, 0, 0);
  print(dt1.millisecondsSinceEpoch); // 0
  print(dt1.microsecondsSinceEpoch); // 0

  final dt2 = DateTime(2023, 5, 30, 8, 48, 45);
  print(dt2.millisecondsSinceEpoch); // 1685411325000
  print(dt2.microsecondsSinceEpoch); // 1685411325000000

  final dt3 = DateTime(1916, 8, 5, 4, 53, 19);
  print(dt3.millisecondsSinceEpoch); // -1685411325000
  print(dt3.microsecondsSinceEpoch); // -1685411325000000

Summary

The DateTime class of Dart already provides the methods for converting between DateTime and Unix epoch timestamp. Just make sure you use the methods with the correct time unit based on the value you have or you want to convert to.

You can also read about: