Dart DateTime toString()
Syntax & Examples


Syntax of DateTime.toString()

The syntax of DateTime.toString() method is:

 String toString() 

This toString() method of DateTime returns a human-readable string for the given DateTime instance.

Return Type

DateTime.toString() returns value of type String.



✐ Example

1 Print current date and time as a human-readable string

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 call the toString() method on now to obtain a human-readable string representation of the date and time.
  3. The resulting string is printed to standard output.

Dart Program

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

Output

2024-05-03 15:02:31.265

Summary

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