Dart Duration toString()
Syntax & Examples
Duration.toString() method
The `toString` method in Dart returns a string representation of this Duration.
Syntax of Duration.toString()
The syntax of Duration.toString() method is:
String toString()
This toString() method of Duration returns a string representation of this Duration.
Return Type
Duration.toString() returns value of type String
.
✐ Examples
1 Duration with days, hours, and minutes
In this example,
- We create a Duration object
duration
with 5 days, 12 hours, and 30 minutes. - We use the
toString()
method to get its string representation. - We then print the result to standard output.
Dart Program
void main() {
Duration duration = Duration(days: 5, hours: 12, minutes: 30);
String durationString = duration.toString();
print('Duration: $durationString');
}
Output
Duration: 5 days, 12:30:00
2 Duration with hours and minutes
In this example,
- We create a Duration object
duration
with 3 hours and 45 minutes. - We use the
toString()
method to get its string representation. - We then print the result to standard output.
Dart Program
void main() {
Duration duration = Duration(hours: 3, minutes: 45);
String durationString = duration.toString();
print('Duration: $durationString');
}
Output
Duration: 3:45:00
3 Duration with minutes
In this example,
- We create a Duration object
duration
with 120 minutes. - We use the
toString()
method to get its string representation. - We then print the result to standard output.
Dart Program
void main() {
Duration duration = Duration(minutes: 120);
String durationString = duration.toString();
print('Duration: $durationString');
}
Output
Duration: 2:00:00
Summary
In this Dart tutorial, we learned about toString() method of Duration: the syntax and few working examples with output and detailed explanation for each example.