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,
- We get the current date and time using the
DateTime.now()
method and store it in the variablenow
. - We then call the
toString()
method onnow
to obtain a human-readable string representation of the date and time. - 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.