title: Powershell Format Duration as Hours and Minutes
body: <p>I am trying to format the string output of a TimeSpan as <code>$h Hours $m Minutes</code>, accounting for pluralization. Right now, I have the below code which works pretty well; however, when it encounters a TimeSpan of less than an hour it will display it as <code>0 $m minutes</code>.</p>
  
  <pre><code>Function FormatDuration ([TimeSpan]$Duration) {
      [string]$DurationStr = [math]::floor($Duration.TotalMinutes/60).ToString()
      if ([math]::floor($Duration.TotalMinutes/60) -eq 1) {
          $DurationStr = $DurationStr+" hour"
      } elseif ([math]::floor($Duration.TotalMinutes/60) -ne 0) {
          $DurationStr = $DurationStr+" hours"
      }
      if ($Duration.Minutes -eq 1){
          $DurationStr = $DurationStr+" "+$Duration.Minutes+" minute"
      # Does not display minutes if value is 0
      } elseif ($Duration.Minutes -ne 0) {
          $DurationStr = $DurationStr+" "+$Duration.Minutes+" minutes"
      }
      return $DurationStr
  }
  </code></pre>
  
  <p>I tried finding if someone else has solved this problem, but the closest I was able to find was zuallauz' <a href="https://stackoverflow.com/a/13505362/7803239">solution</a> in <a href="https://stackoverflow.com/questions/5852305/c-sharp-string-format-for-hours-and-minutes-from-decimal">this C# thread</a>.</p>
  
  <p>Unfortunately, it seems TimeSpans behave differently  in Powershell where they will display days and hours separately, meaning a TimeSpan of 25 hours and 45 minutes will be stored as:</p>
  
  <pre><code>New-TimeSpan -Hours 25 -Minutes 45
  
  Days              : 1
  Hours             : 1
  Minutes           : 45
  Seconds           : 0
  Milliseconds      : 0
  Ticks             : 927000000000
  TotalDays         : 1.07291666666667
  TotalHours        : 25.75
  TotalMinutes      : 1545
  TotalSeconds      : 92700
  TotalMilliseconds : 92700000
  </code></pre>
  
  <p>Can anyone point me in the right direction with how to solve this issue? My first thoughts are to separate processing of hours and minutes and then to concatenate them at the end instead of at each if-else statement.</p>
  
  <h2>Edit:</h2>
  
  <p>With the help of Sambardo's <a href="https://stackoverflow.com/a/49659941/7803239">solution</a>, I was able to create the following function:</p>
  
  <pre><code>Function FormatDuration ([TimeSpan]$Duration) {
      $h = $Duration.Days * 24 + $Duration.Hours
      $m = $Duration.Minutes
  
      switch ($h) {
          0 {$HourStr=$null}
          1 {$HourStr="$h hour"}
          {$_ -ge 1} {$HourStr="$h hours"}
      }
      switch ($m) {
          0 {$MinuteStr=$null}
          1 {$MinuteStr="$m minute"}
          {$_ -ge 1} {$MinuteStr="$m minutes"}
      }
      if ($HourStr -gt 0 -and $MinuteStr -gt 0) {
          [String]$DurationStr=$HourStr+" "+$MinuteStr
      } else {
          $DurationStr=$HourStr+$MinuteStr
      }
  
      return $DurationStr
  }
  </code></pre>
  
  <p>Thank you all for your help!</p>