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
WizradWizrad 

Datetime greater than or equal to oddity

Hi all,

 

I've got a simple test method, within this method I am System.debug()ing out two date time values which are almost always equal, then the next line is an assertion to see if one is >= the other one and it fails.  Code is shown below.

 

 

System.debug('HERE = ' + objList[0].Start_Time__c + ' / ' + timeBeforeExecution);                                     
System.assert(objList[0].Start_Time__c >= timeBeforeExecution);

 

 

Below this is the result of the debug log.

 

 

11:04:31.274|USER_DEBUG|[56]|DEBUG|HERE = 2010-12-09 19:04:29 / 2010-12-09 19:04:29
11:04:31.274|METHOD_EXIT|[56]|System.debug(ANY)
11:04:31.274|METHOD_ENTRY|[57]|System.assert(Boolean)
11:04:31.274|EXCEPTION_THROWN|[57]|System.AssertException: Assertion Failed

 

If I change the assertion to == it works.  Not sure why == would work but not >=.  I guess as a work around I can just assert thats its > || its ==, but I was wondering if anyone had any ideas why this would be happening.

 

Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
WizradWizrad

Looks like a Datetime field value always has a milliseconds of 0, while System.now actually captures the millisecond at the time.

All Answers

mikefmikef

Looks like a bug

 

Then does this work?

 

System.assert(objList[0].Start_Time__c <= timeBeforeExecution);

 

 

WizradWizrad

Looks like a Datetime field value always has a milliseconds of 0, while System.now actually captures the millisecond at the time.

This was selected as the best answer