Skip to content

DateAddHour

See Also: Time and Date Functions

Purpose

The DateAddHour function adds or subtracts a given number of hours to a specified DateTime variable dtVar.

If the added unit exceeds the range of a DateTime section, the entire DateTime is adjusted. For example, adding 48 hours would add 2 days to the DateTime variable. The returned DateTime variable is always valid.

The DateTime variable passed to the function must be valid and non-null. If this is not the case, a DFERR_INVALID_DATETIME error will be raised. You can use the IsDateValid and IsNullDateTime functions to ensure the value you are passing to this function is valid.

This differs from the DateSetXxx functions, which do not perform date arithmetic (i.e., they just set one DateTime section), and neither the incoming nor outgoing DateTime variable is tested for validity.

This function is part of a family of DateTime manipulation functions, which includes: - DateAddDay - DateAddHour - DateAddMillisecond - DateAddMinute - DateAddMonth - DateAddSecond - DateAddYear

The DateSet and CurrentDateTime functions can be used to initialize a DateTime value.

Return Type

DateTime

Syntax

Use GlobalDateTimeFunctions.pkg
DateAddHour( {dtVar}, {iHoursToAdd} )

Where: - {dtVar} is a value of type DateTime. - {iHoursToAdd} is an integer value representing the number of hours to add to or subtract from dtVar.

Example

This example sets the date of a DateTime variable and then adds 1 hour to the time.

Procedure Test
    DateTime dtVar
    Move (DateSet(2015, 9, 26)) to dtVar
    Move (DateAddHour(dtVar, 1)) to dtVar
    // This will print (using US date settings):
    // 9/26/2015 1:00:00 AM
    Showln dtVar
End_Procedure

This example sets the date of a DateTime variable and then subtracts 1 hour from the time.

Procedure Test
    DateTime dtVar
    Move (DateSet(2015, 9, 26)) to dtVar
    Move (DateAddHour(dtVar, -1)) to dtVar
    // This will print (using US date settings):
    // 9/25/2015 11:00:00 PM
    Showln dtVar
End_Procedure

DateAddXxx Functions vs. DateSetXxx

The DateAddXxx functions differ from the DateSetXxx functions, which do not perform date arithmetic (i.e., they just set one DateTime section), and neither the incoming nor outgoing DateTime variable is tested for validity.

In most cases, the DateSet function and the DateAddXxx functions (e.g., DateAddDay, DateAddHour, DateAddMillisecond, DateAddMinute, DateAddMonth, DateAddSecond, DateAddYear) can be used in preference to the DateSetXxx functions (e.g., DateSetDay, DateSetHour, DateSetMillisecond, DateSetMinute, DateSetMonth, DateSetSecond, DateSetYear). The DateSetXxx functions are lower level and should be rarely used.