Dart DateTime toUtc()
Syntax & Examples
Syntax of DateTime.toUtc()
The syntax of DateTime.toUtc() method is:
DateTime toUtc()
This toUtc() method of DateTime returns this DateTime value in the UTC time zone.
Return Type
DateTime.toUtc() returns value of type DateTime
.
✐ Example
1 Convert the current date and time to UTC
In this example,
- We get the current date and time using the
DateTime.now()
method and store it in the variablenow
. - We then use the
toUtc()
method onnow
to convert it to UTC time zone. - The resulting
DateTime
object representing the UTC time is printed to standard output.
Dart Program
void main() {
var now = DateTime.now();
var utcTime = now.toUtc();
print(utcTime);
}
Output
2024-05-03 09:28:44.639Z
Summary
In this Dart tutorial, we learned about toUtc() method of DateTime: the syntax and few working examples with output and detailed explanation for each example.