Why would this statement return true? I understand that I’m comparing a Date to a DateTime variable, but I’m looking for a more technical explanation.
DateTime dt = DateTime.newInstance(2012,04,30,0,0,0);
system.debug(dt > Date.valueOf('2012-04-30'));
Also, would DateTime values (for the dt variable) before 2012-04-30 also return true?
Superfellnoted in the comments that this is probably due to a time-zone conversion as well as midnight (00:00:00) being appended to the Date for comparison. Short story: he was right about it being because of midnight being appended to the Date for the comparison. The detail is below.I needed to see this in action to understand it completely; so, I wrote up a block of code.
The resulting debug log displayed:
The simplist solution would be to compare against
Date.valueOf('2012-05-01');. However, it’s possible to compare against CreatedDate fields using thenewInstanceGMTmethod on the DateTime type. Using the DateTime method, midnight needs to be accounted for.OR
Both methods resulted in the desired results: