Dart DateTime.timestamp()
Syntax & Examples
Syntax of DateTime.timestamp
The syntax of DateTime.DateTime.timestamp constructor is:
DateTime.timestamp()
This DateTime.timestamp constructor of DateTime constructs a DateTime
with the current UTC date and time.
✐ Examples
1 Create a DateTime with current UTC date and time
In this example,
- We create a DateTime named
dateTime1
using theDateTime.timestamp()
constructor. - We print
dateTime1
to standard output, which displays the current date and time in the local time zone.
Dart Program
void main() {
DateTime dateTime1 = DateTime.timestamp();
print(dateTime1);
}
Output
2024-05-08 10:30:00.000
2 Create a DateTime with current UTC date and time
In this example,
- We create a DateTime named
dateTime2
using theDateTime.timestamp()
constructor. - We convert
dateTime2
to UTC usingtoUtc()
before printing, which displays the current date and time in UTC.
Dart Program
void main() {
DateTime dateTime2 = DateTime.timestamp();
print(dateTime2.toUtc());
}
Output
2024-05-08 14:30:00.000Z
3 Get milliseconds since epoch for current DateTime
In this example,
- We create a DateTime named
dateTime3
using theDateTime.timestamp()
constructor. - We access the
millisecondsSinceEpoch
property ofdateTime3
and print it to standard output, which displays the milliseconds since epoch for the current date and time.
Dart Program
void main() {
DateTime dateTime3 = DateTime.timestamp();
print(dateTime3.millisecondsSinceEpoch);
}
Output
1738523000000
Summary
In this Dart tutorial, we learned about DateTime.timestamp constructor of DateTime: the syntax and few working examples with output and detailed explanation for each example.