Dart DateTime millisecondsSinceEpoch
Syntax & Examples
Syntax of DateTime.millisecondsSinceEpoch
The syntax of DateTime.millisecondsSinceEpoch property is:
int millisecondsSinceEpoch
This millisecondsSinceEpoch property of DateTime the number of milliseconds since the "Unix epoch" 1970-01-01T00:00:00Z (UTC).
Return Type
DateTime.millisecondsSinceEpoch returns value of type int
.
✐ Examples
1 Get the number of milliseconds since the Unix epoch for the current DateTime
In this example,
- We create a DateTime object
dt
using theDateTime.now()
constructor, which represents the current date and time. - We then access the
millisecondsSinceEpoch
property ofdt
to get the number of milliseconds since the Unix epoch. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.now();
print(dt.millisecondsSinceEpoch);
}
Output
1714753078948 (Output varies depending on the current date and time.)
2 Get the number of milliseconds since the Unix epoch for a specific DateTime in UTC
In this example,
- We create a DateTime object
dt
using theDateTime.utc()
constructor, specifying the year, month, and day. - We then access the
millisecondsSinceEpoch
property ofdt
to get the number of milliseconds since the Unix epoch. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.utc(2024, 5, 3);
print(dt.millisecondsSinceEpoch);
}
Output
1714694400000
3 Get the number of milliseconds since the Unix epoch for a parsed DateTime
In this example,
- We create a DateTime object
dt
by parsing a date-time string '2024-05-03T10:15:30' using theDateTime.parse()
method. - We then access the
millisecondsSinceEpoch
property ofdt
to get the number of milliseconds since the Unix epoch. - We print the result to standard output.
Dart Program
void main() {
var dt = DateTime.parse('2024-05-03T10:15:30');
print(dt.millisecondsSinceEpoch);
}
Output
1714711530000
Summary
In this Dart tutorial, we learned about millisecondsSinceEpoch property of DateTime: the syntax and few working examples with output and detailed explanation for each example.