function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tzuvytzuvy 

Subtract 5 hours from a Date / Time field

Hi,

 

I'm creating a date / time formula field in which I would like to keep te time of another date time field and subtract five hours from it.

 

For example, in field A I would have "11/23/2010 9:03 PM" and in field B (the formula one I should see "11/23/2010 4:03 PM"

 

 

Is this possible?

 

Thanks

 

Tzuvy

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Use this formula.

fieldA - (1/4.8)

 

Let me know if you find any issues.

All Answers

Imran MohammedImran Mohammed

yes, thats possible.

fieldA - 5  should do the job for fieldB

Imran MohammedImran Mohammed

Hey sorry, i misunderstood your question.

Imran MohammedImran Mohammed

Use this formula.

fieldA - (1/4.8)

 

Let me know if you find any issues.

This was selected as the best answer
tzuvytzuvy

Thanks!!

 

It works perfectly.

 

Cheers

 

Tzuvy

JPlayEHRJPlayEHR

Just in case anybody else was wondering (i.e. you need to subtract or add more or less than 5 hours) 1/4.8 = 5 hours because it is taking 1 day and dividing it by the quotient of 24hours / number of hours. 24 / 5 = 4.8 or 24/4 = 6 (1/6) .  4 hours is 1/6th of a day.  5 hours is 1/4.8th of a day. 

Rahul1Rahul1

Instead of (1/6) You have to use (DateTime - (1/6.0)) for 4 hrs subtraction
(DateTime - (1/8.0)) for 3 hrs subtraction 
(DateTime - (1/12.0)) for 2 hrs subtraction
(DateTime - (1/24.0)) for 1 hrs subtraction


have a nice day!!

AruAruAruAru
How can we substract 5 minutes from DateTime. 
DateTime d = (System.now()- ((24/1440.0)/5.0)) will it work?
Stan VenneStan Venne
I know this is an old post but in case someone pulls this up with a similar question,  the original answers are overcomplicated IMHO.  I propose the following as it more clearly shows the hours you are looking to adjust for based on a 24 hour day.  It may also be easier for you or someone else to understand when reviewing down the road:

fieldA - (5/24) 

A more universal formula to address positive and negative UTC offset:  initialDateField + (<offset hours>/24).  This should also allow you to adjust easily for those UTC offsets that have half-hour or 45-minute increments (additionally adjust by .5 or .75).  Simply replace <offset hours> with your positive or negative offset hours.  In this example initialDateField + (-5/24).

Keep in mind none of these examples account for changes within the year due to daylight savings time.