Salesforce Formula Add Hours to Datetime

Sometimes, you get a requirement to create the Salesforce formula add hours to 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 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 in 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 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.
See also  Salesforce Date Range Formula

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 datetime in Salesforce.

1. Navigate to the Setup, click on the Object Manager, and search for the Object for which data 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.

salesforce formula add hours to datetime

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

addhours in apex

3. Create the Apex Trigger to add 3 hours to the End_Date_Time__c field in the 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, 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.

Follow the below steps to do so:

  • Here, you 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.

salesforce add hours to datetime

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.

add hours to datetime salesforce formula

In this way, you can add hours to datetime in Salesforce using the triggers.

See also  Salesforce Weekday Formula

Salesforce Add Minutes to Datetime

If you want to add minutes to datetime, you can also add it, and it is also possible to do that without knowing any apex. In Salesforce, you can create the formula with the help of standard functions available.

Below are the steps to create a formula to add minutes to datetime in Salesforce.

1. Navigate to the Setup, click on the Object Manager, search for the Object for which record you want to add minutes to hours, and click on it.

salesforce formula add minutes to datetime

2. Click on the Fields & Relationship option from the left sidebar and then click on the New button.

salesforce time field format

3. Choose the Formula as the field type to create the formula to add minutes to the hours and click on the Next button.

salesforce add minutes to datetime

4. Enter the Field Label, and the Field Name will automatically populate. Then, choose the Formula Return Type as DateTime and click on Next.

time field in salesforce

5. Enter the advanced formula that adds minutes to the hour in Salesforce. The formula is as given below:

DATETIMEVALUE(LastModifiedDate) + (10 / (24 * 60))
  • Here, I want to add the 10 minutes to the datetime field Start Time.

Click on the Check Syntax button. Add a Description or Help Text if you want. After that, click on the Next button.

salesforce datetime field

6. Choose the Profile to which you want to grant the edit access of this field and to move to the next step, click on the Next button.

salesforce time format

7. Select the Page Layouts on which you want to add this particular field and click on the Save button.

datetime add hour

You can see that the Next Updated Date has 10 extra minutes as compared to the Last Modified Date.

add minutes to datetime salesforce

In this way you can add the minutes to the datetime in Salesforce.

See also  Salesforce Last Activity Date Formula

Conclusion

I hope you have learned how to add hour to date time field in Salesforce. I will also explain step-by-step how to add minutes to the date time field in Salesforce.

You may also like: