Sometimes, you get a requirement to create a Salesforce formula that adds hours to a datetime. So, in this article, I will explain the Salesforce datetime field. I will also explain step-by-step how to create a formula to add hours to a datetime.
Salesforce Datetime Field
In Salesforce, the Data Type represents which type of data you can store in an object’s field. Date/Time is one of the standard data types in Salesforce. It is used to store both the date and time of the record in a field of the object.
The following Format of the Salesforce Date/Time Field is as given below; you can use any one of them:
. YYYY-MM-DD
. YYYY-MM-DD hh:mm:ss
. YYYY-MM-DDThh:mm:ssZ
. YYYY-MM-DDThh:mm:ss.sssZ
- YYYY represents the year in four digits.
- MM represents the month in two digits.
- DD represents the day in two digits.
- T represents a separator that indicates the start of the time period.
- hh represents the hour in 24 formats in two digits.
- mm represents the minute in two digits.
- ss represents the second in two digits.
- sss represents the milliseconds in three digits, and it is an optional parameter.
- Z represents the time zone in UTC form.
For example, the DateTime Field is 2023-03-25T13:28:02.000Z, which means March 23rd, 2023 at 1:28 PM UTC.
Salesforce Formula Add Hours to Datetime
In Salesforce, to add a specific number of hours to a Date/Time value, you can use the ADDHOURS function. The syntax of the ADDHOURS function is as given below:
ADDHOURS(Date/Time,hours)
The parameters are defined below:
- Date/Time: It represents the date and time value to which we want to add hours.
- Hours: It represents the number of hours we want to add to the DateTime value. The value can be positive or negative as per need.
In addition to this, the addhours function is not the standard Salesforce function. It is only available in the apex in Salesforce, which you can use in the trigger to perform some actions.
Below are the steps to create a formula to add hours to a datetime in Salesforce.
1. Navigate to the Setup, click on the Object Manager, and search for the Object for which you want to add hours to the datetime. For example, choose the Leads object in Salesforce.
After that, from the left-hand menu of the object page, click on the Triggers.
