How to show working hours 2.5 hours like 2 hours and 30 minutes

I have a decimal that shows working hours like 2.5 hours; is it possible to show that as 2 hours and 30 mins? I suppose in a string.

take a look at How to calculate the time lapsed between two dates & times?

If you already have hours in decimal form, the simplest solution is to declare two variables as HOURS & MINUTES. Then

HOURS = Int(Decimal_Hours)

MINUTES = (Decimal_Hours - HOURS) * 60

If you want that exact “# hours and # mins” format you could use a @P pattern picture:

HoursDec   DECIMAL(9,2)   !HH.Decimal
HhhMm      LONG 
HrsMins    STRING(24)

  HhhMm = INT(HoursDec) * 100 |          !Change HHH.MM to  HHH00
        + (HoursDec-INT(HoursDec)) * 60  !add Minutes to be HHHmm
  HrsMins = Left( FORMAT( HhhMm ,  @P<<<# Hours <# MinutesP ) )

You could convert to a Clarion Time but that will be limited to a max of 23 hours 59 minutes

T  TIME

   T = (HoursDec* 60*60*100)  |   !3600 Seconds in 1 Hour *100=360,000
     +  1   !Clarion Time is always +1

1 Like

Hi pbouma,

Yes, you can convert a decimal like 2.5 hours into a string format showing 2 hours and 30 minutes. You can achieve this by separating the integer and decimal parts. Here’s a simple way to do it in Clarion:

DECIMAL hours = 2.5
INTEGER wholeHours = FLOOR(hours)
INTEGER minutes = FLOOR((hours - wholeHours) * 60)
STRING result = FORMAT(wholeHours) + ' hours and ' + FORMAT(minutes) + ' mins'

This will give you the desired output: “2 hours and 30 mins”.

Additionally, if you’re interested in experimenting further with time format management, I’ve added new functions to the ShowTime.clw and Hour.clw files in the ShowTime repository. Here’s a brief overview of the updates:

  • ShowTime.clw: Added functions for enhanced time display formatting and handling time zone conversions, covering everything from years to hundredths of seconds, with both default French and English suffixes, plus the option for user-defined suffixes.
  • Hour.clw: Implemented functions to calculate hours worked, starting from hours down to hundredths of seconds. This includes overtime calculations and hourly rate adjustments, with support for default English and French suffixes.

You can explore the latest updates here: ShowTime Repository.

Hour Function Summary

Purpose: Converts a numeric time value into a formatted string, representing time from hundredths of a second to hours.

Parameters:

  • ilxHour: Time in hundredths of a second (mandatory).
  • sxTnnHourFormat: String for time formatting (optional).
  • sxSuffix: Time suffix unit separator (optional).

Usage:

  • Suitable for any procedure, including menus, reports, and batch processes.
  • Ideal for measuring elapsed time and formatting durations.

Example:

StringVariable = Hour(cTime, '@t1', '/')

Output: Formats the computed time according to specified formats, e.g., 22/15.

Environment: Compatible with all Clarion versions.

Feel free to contribute or provide feedback!