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
zigzagzigzag 

Convert a DateTime field to a Date?

I am trying to create a formula field to hold the day of week; the cookbook helpfully suggests the following nifty formula:

MOD(MyField__c - DATE(1900, 1, 7), 7)

Unfortunately, this only works if MyField__c is a Date field, and not if it is a DateTime field.  It seems silly, but I cannot for the life of me figure out how to convert one to the other - why does it not just do it automatically?

As far as I can tell, none of the date functions work on a DateTime, so I can't just brute force it by extracting the year, month and day to create a new Date like this:

DATE(YEAR(MyField__c), MONTH(MyField__c), DAY(MyField__c)

Any advice?  Hopefully there is a workaround - surely a DateTime is just a Date that also has a timestamp attached to it...

Thanks
Best Answer chosen by Admin (Salesforce Developers) 
zigzagzigzag
The solution is simple:

DATEVALUE(MyField__c)

returns a Date value that I can work with.  Why DATE(MyField__c) doesn't just do the right thing I don't know though :)

Thanks,
Allan