Dart DateTime.fromMicrosecondsSinceEpoch()
Syntax & Examples


Syntax of DateTime.fromMicrosecondsSinceEpoch

The syntax of DateTime.DateTime.fromMicrosecondsSinceEpoch constructor is:

DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, {bool isUtc = false})

This DateTime.fromMicrosecondsSinceEpoch constructor of DateTime constructs a new DateTime instance with the given microsecondsSinceEpoch.

Parameters

ParameterOptional/RequiredDescription
microsecondsSinceEpochrequiredThe number of microseconds since the Unix epoch (1 January 1970 00:00:00.000 UTC).
isUtcoptional [default value is false]Whether the given time is in UTC or local time. If true, the resulting DateTime object represents a time in UTC; if false, it represents a time in the local time zone.


✐ Examples

1 Create DateTime object from microseconds since epoch

In this example,

  1. We define a variable microseconds with a value representing the number of microseconds since the Unix epoch.
  2. We then use the DateTime.fromMicrosecondsSinceEpoch() constructor to create a DateTime object.
  3. The resulting DateTime object represents the time corresponding to the provided number of microseconds since the epoch.
  4. We print the DateTime object to standard output.

Dart Program

void main() {
  var microseconds = 1620067692801;
  var dateTime = DateTime.fromMicrosecondsSinceEpoch(microseconds);
  print(dateTime);
}

Output

2021-05-03 14:21:32.801

2 Create DateTime object from microseconds since epoch in UTC

In this example,

  1. We define a variable microseconds with a value representing the number of microseconds since the Unix epoch.
  2. We then use the DateTime.fromMicrosecondsSinceEpoch() constructor with the isUtc parameter set to true to create a DateTime object representing the time in UTC.
  3. The resulting DateTime object represents the time corresponding to the provided number of microseconds since the epoch in UTC.
  4. We print the DateTime object to standard output.

Dart Program

void main() {
  var microseconds = 1620067692801;
  var dateTime = DateTime.fromMicrosecondsSinceEpoch(microseconds, isUtc: true);
  print(dateTime);
}

Output

2021-05-03 14:21:32.801Z

3 Create DateTime object from microseconds since epoch in local time

In this example,

  1. We define a variable microseconds with a value representing the number of microseconds since the Unix epoch.
  2. We then use the DateTime.fromMicrosecondsSinceEpoch() constructor with the isUtc parameter set to false (or omitted, as it defaults to false) to create a DateTime object representing the time in the local time zone.
  3. The resulting DateTime object represents the time corresponding to the provided number of microseconds since the epoch in the local time zone.
  4. We print the DateTime object to standard output.

Dart Program

void main() {
  var microseconds = 1620067692801;
  var dateTime = DateTime.fromMicrosecondsSinceEpoch(microseconds, isUtc: false);
  print(dateTime);
}

Output

2021-05-03 10:21:32.801

Summary

In this Dart tutorial, we learned about DateTime.fromMicrosecondsSinceEpoch constructor of DateTime: the syntax and few working examples with output and detailed explanation for each example.