The HotDocs Computation Archive
Get Extra Help

0124 - Time + 1 HOUR

Description:

Simple time calculations - add or subtract an hour.


• Explanation •

Before you can do time calculations, you must get the hours and minutes into separate number variables. In the examples below, these number variables are called Hour-n and Min-n. Ideally you would have the user input values directly into these number variables, but it may be that the time value is in a text variable (such as "2:30 p.m."). If this is the case, you must first use Computation #0099: Parsing Time Values to extract the Hour-n and Min-n values.

The computations below assume that the time is in military format (1:00 pm = 13:00). If this is not the case, you must first use Computation #0102: Convert Hours to Military Time to make the conversion.


• Code •

Add An Hour:

// Add one hour
SET Hour-n TO Hour-n + 1
IF Hour-n > 23
   SET Hour-n TO Hour-n - 24
END IF

// Format the computed time
IF Hour-n < 12
   IF Hour-n = 0
      SET Hour-n TO 12
   END IF
   "«Hour-n»:«Min-n:09» a.m."
ELSE
   IF Hour-n > 12
      SET Hour-n TO Hour-n - 12
   END IF
   "«Hour-n»:«Min-n:09» p.m."
END IF
 
 
Subtract An Hour:

SET Hour-n TO Hour-n - 1
IF Hour-n < 0
   SET Hour-n TO Hour-n + 24
END IF

// Format the computed time
IF Hour-n < 12
   IF Hour-n = 0
      SET Hour-n TO 12
   END IF
   "«Hour-n»:«Min-n:09» a.m."
ELSE
   IF Hour-n > 12
      SET Hour-n TO Hour-n - 12
   END IF
   "«Hour-n»:«Min-n:09» p.m."
END IF
 
 
Add An Hour - Date Relevant:

// Add one hour
SET Hour-n TO Hour-n + 1
IF Hour-n > 23
   SET Hour-n TO Hour-n - 24
   SET DateVar TO DateVar + 1 DAY
END IF

// Format the computed time
IF Hour-n < 12
   IF Hour-n = 0
      SET Hour-n TO 12
   END IF
   "«Hour-n»:«Min-n:09» a.m."
ELSE
   IF Hour-n > 12
      SET Hour-n TO Hour-n - 12
   END IF
   "«Hour-n»:«Min-n:09» p.m."
END IF

These examples show how to add or subtract one hour from a time value. The process is straightforward. An hour is added or subtracted. The result is then analyzed to see if it has crossed over into another day (11:30 pm -> 12:30 am, for example). If it has, the hour is adjusted accordingly. Finally, a formatted text string is returned in proper am/pm format (rather than military time). The first two examples ignore the impact of a date change. The final example shows how to have a date variable adjusted as well if the calculation results in a date change.

 

• Contributors •

LegalCS