Dart DateTime microsecondsSinceEpoch
Syntax & Examples


Syntax of DateTime.microsecondsSinceEpoch

The syntax of DateTime.microsecondsSinceEpoch property is:

 int microsecondsSinceEpoch 

This microsecondsSinceEpoch property of DateTime the number of microseconds since the "Unix epoch" 1970-01-01T00:00:00Z (UTC).

Return Type

DateTime.microsecondsSinceEpoch returns value of type int.



✐ Examples

1 Get the number of microseconds since the Unix epoch for the current DateTime

In this example,

  1. We create a DateTime object dt using the DateTime.now() constructor, which represents the current date and time.
  2. We then access the microsecondsSinceEpoch property of dt to get the number of microseconds since the Unix epoch.
  3. We print the result to standard output.

Dart Program

void main() {
  var dt = DateTime.now();
  print(dt.microsecondsSinceEpoch);
}

Output

1714753220834000
(Output varies depending on the current date and time.)

2 Get the number of microseconds since the Unix epoch for a specific DateTime in UTC

In this example,

  1. We create a DateTime object dt using the DateTime.utc() constructor, specifying the year, month, and day.
  2. We then access the microsecondsSinceEpoch property of dt to get the number of microseconds since the Unix epoch.
  3. We print the result to standard output.

Dart Program

void main() {
  var dt = DateTime.utc(2024, 5, 3);
  print(dt.microsecondsSinceEpoch);
}

Output

1714694400000000

3 Get the number of microseconds since the Unix epoch for a parsed DateTime

In this example,

  1. We create a DateTime object dt by parsing a date-time string '2024-05-03T10:15:30' using the DateTime.parse() method.
  2. We then access the microsecondsSinceEpoch property of dt to get the number of microseconds since the Unix epoch.
  3. We print the result to standard output.

Dart Program

void main() {
  var dt = DateTime.parse('2024-05-03T10:15:30');
  print(dt.microsecondsSinceEpoch);
}

Output

1714711530000000

Summary

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