+ Start a Discussion
EagerToLearnEagerToLearn 

CreatedDate not returning or able to query correctly form SOQL

I am having an issue getting a SOQL query to return the proper results when the WHERE clause is using the CreatedDate.  I hear about the UTC/GTM but don't fully understand how to get the proper results.

Today is 7/21 and I have records that show on the standard SF page for Created Date as 7/20/2018 and 7/21/2018; however, when I use in code or the Developer Console I only see CreatedDate values of 7/21/218Tnn:nn:nn.000+0000.
 

If I us the following WHERE clause (...WHERE CreatedDate < 2018-07-21T00:00:00Z) I get no records back but if I change the 21 I do get records back but again they all show as 07/21...

However, when I use the following WHERE clause (WHERE CreatedDate < TODAY) I get records back but they still show as 07/21/2018...

From my code persective I am trying to delete record based on a specific date in the past.and although the requirement is that that specific that it can't be off a bit but this issue is really bothering me that I can't easily delete the records starting on a specific date using a query in apex like below and accurately.

DELETE [ SELECT id  from mytablename__c WHERE CreatedDate < :calculatedDate LIMIT 9999];


Your help is very much appreciated - thank you in advance.

Steven NsubugaSteven Nsubuga
Apply the format() to all the returned and calculated date times. It is not an ideal solution but I reckon it can get you closer to a solution.
 
System.debug('now ='+datetime.now());  // returns current date time in UTC
System.debug('now formatted ='+datetime.now().format()); // returns current date time as a date string in your time zone

mytablename__c mine = [ SELECT id, CreatedDate  from mytablename__c];
Datetime formattedDatetime = DateTime.parse(mine.CreatedDate.format());
DateTime calculatedDate = DateTime.parse(DateTime.newInstance(1993, 6, 6, 3, 3, 3).format());

if (formattedDatetime > calculatedDate) {
       // logic
}