DateTime and Date Comparison

15,779

Superfell noted 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.

for(Integer mo=4; mo<6; mo++) // test April (4) and May (5) only
    for(Integer d=(mo==4?29:0); d<=(mo==5?2:30); d++) // test between 2012-04-29 and 2012-05-30
        for(Integer h=0; h<12; h++)
            for(Integer m=0; m<60; m++)
            {
                // this simulates a CreatedDate field (note it is generated using the newInstanceGMT method and not simply newInstance)
                // DateTime fields in Salesforce are stored in GMT then converted to the current User's time-zone. 
                DateTime dt1 = DateTime.newInstanceGMT(2012,mo,d,h,m,0); 
                Date dt2 = Date.valueOf('2012-04-30');
                if(dt1 > dt2) { 
                    system.debug(LoggingLevel.INFO,'DateTime:'+dt1+' > Date:'+dt2); }
            }

The resulting debug log displayed:

USER_DEBUG|[9]|INFO|DateTime:2012-04-30 00:01:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-04-30 00:02:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-04-30 00:03:00 > Date:2012-04-30 00:00:00
...
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:57:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:58:00 > Date:2012-04-30 00:00:00
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:59:00 > Date:2012-04-30 00:00:00

The simplist solution would be to compare against Date.valueOf('2012-05-01');. However, it's possible to compare against CreatedDate fields using the newInstanceGMT method on the DateTime type. Using the DateTime method, midnight needs to be accounted for.

DateTime dt1 = DateTime.newInstanceGMT(2012,mo,d,h,m,0); // simulated CreatedDate field
Date dt2 = Date.valueOf('2012-05-01'); 
if(dt1 > dt2) { 
    system.debug(LoggingLevel.INFO,'DateTime:'+dt1+' > Date:'+dt2); }

OR

DateTime dt1 = DateTime.newInstanceGMT(2012,mo,d,h,m,0); // simulated CreatedDate field
DateTime dt2 = DateTime.newInstanceGMT(2012,04,30,12,59,59); 
if(dt1 > dt2) { 
    system.debug(LoggingLevel.INFO,'DateTime:'+dt1+' > Date:'+dt2); }

Both methods resulted in the desired results:

USER_DEBUG|[9]|INFO|DateTime:2012-05-01 00:00:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-01 00:01:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-01 00:02:00 > Date:2012-04-30 12:59:59
...
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:57:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:58:00 > Date:2012-04-30 12:59:59
USER_DEBUG|[9]|INFO|DateTime:2012-05-02 11:59:00 > Date:2012-04-30 12:59:59
Share:
15,779
Matt K
Author by

Matt K

Updated on July 26, 2022

Comments

  • Matt K
    Matt K almost 2 years

    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?