Dart Duration inHours
Syntax & Examples


Duration.inHours property

The `inHours` property in Dart returns the number of whole hours spanned by a Duration object.


Syntax of Duration.inHours

The syntax of Duration.inHours property is:

 int inHours 

This inHours property of Duration returns the number of whole hours spanned by this Duration.

Return Type

Duration.inHours returns value of type int.



✐ Examples

1 Calculating hours from days and hours

In this example,

  1. We create a Duration object duration with 2 days and 3 hours.
  2. We get the total hours using the inHours property.
  3. We then print the total hours to standard output.

Dart Program

void main() {
  Duration duration = Duration(days: 2, hours: 3);
  int hours = duration.inHours;
  print('Total hours: $hours');
}

Output

Total hours: 51

2 Calculating hours from hours and minutes

In this example,

  1. We create a Duration object duration with 10 hours and 30 minutes.
  2. We get the total hours using the inHours property.
  3. We then print the total hours to standard output.

Dart Program

void main() {
  Duration duration = Duration(hours: 10, minutes: 30);
  int hours = duration.inHours;
  print('Total hours: $hours');
}

Output

Total hours: 10

3 Calculating hours from minutes

In this example,

  1. We create a Duration object duration with 150 minutes.
  2. We get the total hours using the inHours property.
  3. We then print the total hours to standard output.

Dart Program

void main() {
  Duration duration = Duration(minutes: 150);
  int hours = duration.inHours;
  print('Total hours: $hours');
}

Output

Total hours: 2

Summary

In this Dart tutorial, we learned about inHours property of Duration: the syntax and few working examples with output and detailed explanation for each example.