Dart DateTime toIso8601String()
Syntax & Examples


Syntax of DateTime.toIso8601String()

The syntax of DateTime.toIso8601String() method is:

 String toIso8601String() 

This toIso8601String() method of DateTime returns an ISO-8601 full-precision extended format representation.

Return Type

DateTime.toIso8601String() returns value of type String.



✐ Examples

1 Convert the current date and time to ISO-8601 format

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 toIso8601String() method on now to convert it to an ISO-8601 formatted string.
  3. The resulting string is printed to standard output.

Dart Program

void main() {
  var now = DateTime.now();
  var isoString = now.toIso8601String();
  print(isoString);
}

Output

The current date and time in ISO-8601 format.

2 Convert the current date and time to ISO-8601 format

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 toIso8601String() method on now to convert it to an ISO-8601 formatted string.
  3. The resulting string is printed to standard output.

Dart Program

void main() {
  var now = DateTime.now();
  var isoString = now.toIso8601String();
  print(isoString);
}

Output

The current date and time in ISO-8601 format.

3 Convert the current date and time to ISO-8601 format

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 toIso8601String() method on now to convert it to an ISO-8601 formatted string.
  3. The resulting string is printed to standard output.

Dart Program

void main() {
  var now = DateTime.now();
  var isoString = now.toIso8601String();
  print(isoString);
}

Output

The current date and time in ISO-8601 format.

Summary

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