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
Parameter | Optional/Required | Description |
---|---|---|
microsecondsSinceEpoch | required | The number of microseconds since the Unix epoch (1 January 1970 00:00:00.000 UTC). |
isUtc | optional [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,
- We define a variable
microseconds
with a value representing the number of microseconds since the Unix epoch. - We then use the
DateTime.fromMicrosecondsSinceEpoch()
constructor to create a DateTime object. - The resulting DateTime object represents the time corresponding to the provided number of microseconds since the epoch.
- 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,
- We define a variable
microseconds
with a value representing the number of microseconds since the Unix epoch. - We then use the
DateTime.fromMicrosecondsSinceEpoch()
constructor with theisUtc
parameter set totrue
to create a DateTime object representing the time in UTC. - The resulting DateTime object represents the time corresponding to the provided number of microseconds since the epoch in UTC.
- 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,
- We define a variable
microseconds
with a value representing the number of microseconds since the Unix epoch. - We then use the
DateTime.fromMicrosecondsSinceEpoch()
constructor with theisUtc
parameter set tofalse
(or omitted, as it defaults tofalse
) to create a DateTime object representing the time in the local time zone. - The resulting DateTime object represents the time corresponding to the provided number of microseconds since the epoch in the local time zone.
- 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.