1

I have a datetime field field1 which i am populating using NOW() formula. At some point later i want to know the difference between NOW() and field1 in number of days.

I tried NOW()-field1 and DATETIMEVALUE(NOW()) - DATETIMEVALUE(field1), but it does not seem to be correct.

I am storing this result in a number field which can store 6 decimal places. But when i multiply the value by 60*24, it does not return correct result. If the difference is 12 hours between NOW and field1, I want the field to have .5 as value. If the difference is 6 hours, it should have .25

  • NOW() - DateTime_Field__c works very well for me. Why do you mean it isn't? – Sergej Utko Mar 14 '16 at 14:19
2

I think your approach is right, but you should use only 2 decimal places in your formula number field.

I have tested the following:

  • NOW() field (DateTime formula)
  • DateTime field
  • Difference field with formula NOW()- ut__Datetime__c (number formula, 2 decimal places)

Works well:

enter image description here

2

You can use this.

Now()-Field1__c - FLOOR(Now()-Field1__c)

For more info go to the Link

2

If you use it in Apex, this works:

(f2.getTime() - f1.getTime()) / 1000 / 60 / 60 / 24;

first you calculate difference in milliseconds between datetimes, then get number of days from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.