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,

  1. We get the current date and time using the DateTime.now() method and store it in the variable now.
  2. We then use the toUtc() method on now to convert it to UTC time zone.
  3. 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.