The “time data type (Time)” is handled in many scenarios such as in Salesforce formulas.
For example, time manipulation is necessary in many cases, such as obtaining the “current time” when a process is executed, calculating the difference from a specific time, or adding or subtracting time in seconds or milliseconds.
This article provides a systematic summary of convenient functions and techniques you should know when handling time in Salesforce.
Specifically, it covers the following content.
Understanding these enables more flexible and accurate time calculations and logic construction.
Obtaining the current time
The current time can be obtained using the TIMENOW function.
TIMENOW()
Adding to or subtracting from time
Time data type additions and subtractions are made in millisecond units.
Take care of the units when you want to obtain a time h hours later, m minutes later, s seconds later or ms milliseconds later.
Time data type + ( ( h * 60 + m ) * 60 + s ) * 1000 + ms
Calculating hours, minutes, seconds and milliseconds from the time
The HOUR function, MINUTE function, SECOND function and MILLISECOND function are used to calculate hours, minutes, seconds and milliseconds from the time.
The following format is used to obtain the hours.
HOUR( time datatype )
The following format is used to obtain the minutes.
MINUTE( time data type)
The following format is used to obtain the seconds.
SECOND( time data type)
The following format is used to obtain the milliseconds.
MILLISECOND( time data type)
Calculating the time from hours, minutes, seconds and milliseconds
The TIMEVALUE function is used to obtain the time from hours, minutes, seconds and milliseconds.
The TIMEVALUE function passes the hours, minutes, seconds and milliseconds as a string.
TIMEVALUE( "HH:MM:SS.SSS" )
If the hours, minutes, seconds and milliseconds are obtained as a number, this must be converted to a string using the TEXT function.
TIMEVALUE( TEXT( HH ) + ":" + TEXT( MM ) + ":" + TEXT( SS ) + "." + TEXT( SSS ) )
Calculating the time from milliseconds
The TIMEVALUE function is used to obtain the time from hours, minutes, seconds and milliseconds.
TIMEVALUE(
TEXT( FLOOR( ms / 60 / 60 / 1000 ) )
+ ":" +
TEXT( FLOOR( ms / 60 / 1000 ) - FLOOR( ms / 60 / 60 / 1000 ) * 60 )
+ ":" +
TEXT( FLOOR( ms / 1000 ) - FLOOR( ms / 60 / 1000 ) * 60 )
+ "." +
TEXT( ms - FLOOR( ms / 1000 ) * 1000 )
)
This can be represented concisely using the FORMATDURATION function.
Note that the FORMATDURATION function requires seconds as an argument and returns a value in the format `HH:MM:SS`.
TIMEVALUE( FORMATDURATION( FLOOR( ms / 1000 ) ) + "." + TEXT( ms - FLOOR( ms / 1000 ) * 1000 ) )
Calculating time differences
Since time differences are calculated in milliseconds, a coefficient must be applied when unit adjustments are required.
The following format is used to obtain it in millisecond units.
( time data type - time data type)
The following format is used to obtain it in second units.
( time data type - time data type) / 1000
The following format is used to obtain it in minute units.
( time data type - time data type) / 60 / 1000
The following format is used to obtain it in hour units.
( time data type - time data type) / 60 / 60 / 1000
Obtaining the time difference in the time data type is described using the TIMEVALUE function.
TIMEVALUE(
TEXT( FLOOR( ( time data type - time data type) / 60 / 60 / 1000 ) )
+ ":" +
TEXT( FLOOR( ( time data type - time data type) / 60 / 1000 ) - FLOOR( ( time data type - time data type) / 60 / 60 / 1000 ) * 60 )
+ ":" +
TEXT( FLOOR( ( time data type - time data type) / 1000 ) - FLOOR( ( time data type - time data type) / 60 / 1000 ) * 60 )
+ "." +
TEXT( ( time data type - time data type) - FLOOR( ( time data type - time data type) / 1000 ) * 1000 )
)
Since this is similar to calculating time from milliseconds, it can be written concisely using the FORMATDURATION function.
TIMEVALUE( FORMATDURATION( FLOOR( ( time data type - time data type) / 1000 ) ) + "." + TEXT( ( time data type - time data type) - FLOOR( ( time data type - time data type) / 1000 ) * 1000 ) )
005115576

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.