Dart DateTime millisecond
Syntax & Examples


Syntax of DateTime.millisecond

The syntax of DateTime.millisecond property is:

 int millisecond 

This millisecond property of DateTime the millisecond [0...999].

Return Type

DateTime.millisecond returns value of type int.



✐ Examples

1 Get current millisecond

In this example,

  1. We create a DateTime object now with the current date and time using DateTime.now().
  2. We access the millisecond property of now to get the current millisecond value.
  3. We print the current millisecond value to standard output.

Dart Program

void main() {
  DateTime now = DateTime.now();
  int currentMillisecond = now.millisecond;
  print('Current millisecond: $currentMillisecond');
}

Output

Current millisecond: 142
(The output may change based on the current time.)

2 Get millisecond value from a specific date

In this example,

  1. We create a DateTime object date representing a specific date and time (2024-05-03 15:30:45.678 in this example).
  2. We access the millisecond property of date to get the millisecond value for that date.
  3. We print the millisecond value to standard output.

Dart Program

void main() {
  DateTime date = DateTime(2024, 5, 3, 15, 30, 45, 678);
  int millisecondValue = date.millisecond;
  print('Millisecond value: $millisecondValue');
}

Output

Millisecond value: 678

3 Get millisecond value at midnight

In this example,

  1. We create a DateTime object midnight representing midnight on a specific date (2024-05-03 in this example).
  2. We access the millisecond property of midnight to get the millisecond value at midnight for that date.
  3. We print the midnight millisecond value to standard output.

Dart Program

void main() {
  DateTime midnight = DateTime(2024, 5, 3, 0, 0, 0, 0);
  int midnightMillisecond = midnight.millisecond;
  print('Midnight millisecond: $midnightMillisecond');
}

Output

Midnight millisecond: 0

Summary

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