0

I have a custom field with data type "Date" and when I run a query in the dev console the date returns as expected

SELECT customfield_date__d from myobject

2017-10-03

When I query against the custom object using a List in Apex the field in the List is now in DateTime

List<myobject> myrows = [SELECT customfield_date__d from myobject]

Debug log

VARIABLE_ASSIGNMENT|[62]|myrows|"2017-10-03T00:00:00.000Z"

If I display the field in the log (system.debug()), the results is strange; the "T" and ".000Z" are removed

system.debug(myrows[0].customfield_date__d);

2017-10-04 00:00:00

I'm not exactly clear on all the conversion going on, but the result I need is the custom field in date format

2017-10-03

I need to compare the date to 60 days ago for all the rows in myobject and with all these conversions I"m getting stuck on how to proceed.

1
  • what is your current approach? where is your code? where re you stuck? as is, this might be a possible duplicate of Convert DateTime to Date
    – glls
    Commented Oct 4, 2017 at 0:52

1 Answer 1

1

It's really just how the system behaves. Date values are just DateTime values with a midnight component, and that's how the system debugs Date values (via String.valueOf(Date)). They all mean exactly the same thing. If you want to compare to sixty days ago, just get a date:

Date sixtyDaysAgo = Date.today().addDays(-60);
if(sixtyDaysAgo > myrows[0].customfield_date__c) {
    // was more than sixty days ago
} else {
    // sixty or less days ago
}

Not the answer you're looking for? Browse other questions tagged .