Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7878907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:47:05+00:00 2026-06-03T03:47:05+00:00

Why would this statement return true? I understand that I’m comparing a Date to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:47:07+00:00Added an answer on June 3, 2026 at 3:47 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you do this PHP switch statement? Also note that these are much
I am looking for a function that will return true / false if the
I can't quite figure out why this Linq Statement isn't working as i would
I would like to know how to write this inside my hittest if statement:
Would this be the correct way of declaring that an XML element Cluster contains
Why would this work timeout 10s echo foo bar # foo bar but this
This is my code and im getting an unreachable statement error on it but
This is a rather basic question regarding the syntax of the return statement in
I would have expected it to return 'true' or 'false'... I have overridden OnAuthorization
I am looking for the correct way to handle a return statement with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.