I am not sure if below is bug in salesforce or is expected behaviour and would like to know reason for same.

If I create a formula field as below

Type - Date and formula only containing CreatedDate as show in below image. enter image description here

I was expecting I will receive error to use DATEVALUE as CreatedDate is DateTime field and formula is created with Date as type but it was saved. Moreover on adding same field on all layouts and checking data in SOQL it only shows Date part of CreatedDate field(DateTime)

Layout :

enter image description here

SOQL Result:

enter image description here

Only time I was able to view actual date time value was in Apex.

enter image description here

Another catch is although it is appearing as Date in SOQL and UI but if you try to query solely by passing date it wont give you result for example :

Query :

SELECT Id,Test_Field__c,CreatedDate from account where Test_Field__c = 2022-01-04

This will give no results.Unless I use ">" instead of "=" which would make sense only because of time component which somehow gets hidden

  • DateTime is auto converted to Date in most scenarios. The other way also happens but beware it creates a DateTime of midnight in UTC, not user timezone.
    – Phil W
    Jan 11 at 13:08
  • Isnt it wrong to allow implicit conversion ?
    – elijah
    Jan 11 at 15:50
  • 1
    It is as it is. Implicit conversion, especially from Date to DateTime, is "dangerous" and something you should avoid relying on. Sadly, it's up to the implementer/developer/admin to be aware and avoid.
    – Phil W
    Jan 11 at 16:11
  • Yep after running across this scenarios ,I realised its a very big gap which a developer might not recognise very easily and needs to be cautious when working around date and date time fields
    – elijah
    Jan 11 at 17:04

Your Answer

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

Browse other questions tagged or ask your own question.