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,
- We get the current date and time using the
DateTime.now()method and store it in the variablenow. - We then use the
toIso8601String()method onnowto convert it to an ISO-8601 formatted string. - 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,
- We get the current date and time using the
DateTime.now()method and store it in the variablenow. - We then use the
toIso8601String()method onnowto convert it to an ISO-8601 formatted string. - 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,
- We get the current date and time using the
DateTime.now()method and store it in the variablenow. - We then use the
toIso8601String()method onnowto convert it to an ISO-8601 formatted string. - 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.