In this Salesforce Tutorial, we will learn how to Add Hour to DateTime Field in Salesforce. Moreover, we will go through the steps using which we can Add Hour to DateTime Field in Salesforce Lightning and Salesforce Classic.
In Salesforce, there are many scenarios where we need to Add Hour to DateTime in Salesforce, such as scheduling a task, calculating the end or start time of a campaign, managing business meeting hours, etc.
And, In all these cases, we need to Add the Hour to DateTime field in Salesforce. So, If you want to explore How to Add an Hour to DateTime Field in Salesforce Lightning and Classic Edition, you have come to the right place.
The following are the topics that we will cover in this Salesforce Tutorial:
- Salesforce Date/Time Field
- Salesforce AddHours Function
- Add Hour to DateTime Field in Salesforce Lightning using Triggers
- Add Hour to DateTime Field in Salesforce Classic using Triggers
Salesforce Date/Time Field
In Salesforce, the Data Type is used to represent which type of data we can store in the field of an object. And 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. And to store data in the Date/Time Field, we use the standard date and time format.
The following Format of the Salesforce Date/Time Field is as given below, we 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.
With, this we have learned about Salesforce Date/Time Field. Now, we will move ahead and learn about the Salesforce AddHours function.
Salesforce AddHours() Function
In Salesforce, to add a specific number of hours in a Date/Time value, we 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 whom 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.
Let’s understand it in detail with the help of an example:
Suppose, we want to create a formula that adds 3 hours to the start date and time field to calculate the end date and time of the meeting.
Here, is the formula:
ADDHOURS(Start_Time_c, 3)
With, this we have learned about the Salesforce AddHours function. Now, we will move further and see How to Add Hour to DateTime field in Salesforce Lightning.
Add Hour to DateTime Field using Triggers in Salesforce Lightning
Here are the steps to Add the Hour to Date/Time field using a trigger in Salesforce Lightning.
- Log in to your Salesforce account and navigate to Setup.
- On the left-hand menu, click on Object Manager.

- Click on the object for which we want to add an hour to the DateTime field. Here, I select the Lead object from the list of objects.

- In the left-hand menu of the object page, click on the Triggers.

- Click on the New button to create a new trigger to add an hour to the DateTime field.

- Create the Apex Trigger to add three hours to the End_Date_Time__c field in Salesforce Lead Object. Past the below-given code:
trigger LeadTrigger on Lead( before insert ) {
for ( Lead objLead : trigger.new ) {
if ( objLead.Start_Date_Time__c != null ) {
objLead.End_Date_Time__c = objLead.Start_Date_Time__c.addHours( 3 );
}
}
}
In the Salesforce Apex code, we create a trigger that executes before an event occurs on the lead record. It automatically adds the three hours to the End_Date_Time__c field of the Salesforce Lead Object.
Here, we create a trigger with the unique name ‘LeadTrigger’ which is associated with the ‘Lead’ object, and event the trigger ‘before insert’.
It adds the 3 hours to the custom field called ‘End_Date_Time__c’ with respect to the ‘Start_Date_Time__c’ custom field using the addHours function.
- To save the trigger, click on the Save button.

- Once the trigger is created, open the Lead object and create a new lead with the Start Date Time value and save it. After that, click on the Details section and here we will get the End Date Time field with a value having 3 more hours.

With this, we have learned to add the Hour to DateTime field using Trigger in Salesforce Lightning. Next, we will see how we can add an Hour to DateTime field using Trigger in Salesforce Classic.
Read Create Custom Picklist Field in Salesforce
Add Hour to DateTime Field using Triggers in Salesforce Classic
Here are the steps to Add the Hour to Date/Time field using a trigger in Salesforce Classic.
- Click on Avtar and then select Switch to Salesforce Classic to log in to our Salesforce Classic account.

- Go to the Setup menu in Salesforce Classic.

- Click on the Build section.
- Then, under Customize section select the Object for which we want to add an hour to the DateTime field. Here, I select the Price Books object from the list of objects.
- Click on the dropdown arrow and select Triggers.

- To create a new trigger to add an hour to the DateTime field, click on the New button.

- Create the Apex Trigger to add 1 hour to the End_Offer_Date_Time__c field in Salesforce Price Books object. Past the below-given code:
trigger PriceBookTrigger on Pricebook2( before insert ) {
for ( Pricebook2 objPricebook : trigger.new ) {
if ( objPricebook.Start_Offer_Date_Time__c != null ) {
objPricebook.End_Offer_Date_Time__c = objPricebook.Start_Offer_Date_Time__c.addHours( 1 );
}
}
}
In the Salesforce Apex code, we create a trigger that executes before an event occurs on the price book record. It automatically adds the one hour to the End_Offer_Date_Time__c field of the Salesforce Price Books Object.
Here, we create a trigger with the unique name ‘PriceBookTrigger’ which is associated with the ‘Price Books’ object, and event the trigger ‘before insert’.
It adds the 1 hour to the custom field called ‘End_Offer_Date_Time__c’ with respect to the ‘Start_Offer_ Date_Time__c’ custom field using the addHours function.
- To save the trigger, click on the Save button.

- Once the trigger is created, open the Price Books object and choose any price book record. We will get the End Offer Date Time field with a value having one more hour.

With this, we have learned to add the Hour to DateTime field using Trigger in Salesforce Classic.
Conclusion
We have learned about Salesforce DateTime Fied. In addition, we have also explored the Salesforce ADDHOURS function which is used to add or minus certain hours from the DateTime field.
Moreover, we have learned that both Salesforce Classic and Salesforce Lightning provide a simple procedure to Add Hours to DateTime Field.
In addition to this, the following are the topics that we have discussed:
- Salesforce Date/Time Field
- Salesforce AddHours Function
- Add Hour to DateTime Field in Salesforce Lightning using Triggers
- Add Hour to DateTime Field in Salesforce Classic using Triggers
You may also like:
- How to enable notes in Salesforce
- Report Tab not visible in Salesforce
- How to create Lead Record Type in Salesforce
- Add Days to Date in Salesforce Formula
- Salesforce Formula Add Year to Date
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.