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
Payam EzatpoorPayam Ezatpoor 

Date fields are returned like DateTime when using Dynamic SOQL and .get method. Any fix or workaround?

When you use get method to dynamically pull in information from a Dynamic SOQL and you are iterating through the values so the data can be Date, Text, Picklist or any other data types, the data is returned as Date Time with the '00:00:00' added to the end of it. 

I have seen the solution of splitting the string and cut the hour from the data time value but this workaround doesn't work when you are looping through different kinds of data. Is there anything to help with fixing this issue?

Here is what is happening if you run the following:
 
// DateValue__c has the value of 1/1/2018 (Data Type is Date)

Object__c test = [SELECT DateValue__c FROM Object__c WHERE Id=1234];

System.debug(String.valueof(test.DateValue__c));
// Shows : 2018-01-01 (Correct)


String dynamicSOQL = 'SELECT DateValue__c FROM Object__c WHERE Id=1234';
Object__c dynamicSOQLResult = Database.query( dynamicSOQL );

System.debug(String.valueof(dynamicSOQLResult.DateValue__c));
// Shows: 2018-01-01 (Correct)

System.debug(String.valueof(dynamicSOQLResult.get('DateValue__c')));
// Shows : 2018-01-01 00:00:00 (Wrong)