+ Start a Discussion
Matt Scholl 10Matt Scholl 10 

System Datetime Now() 12 hours off

Hi Everyone,
I have an Apex class that records the datetime certain fields change. On one field, System.Datetime.now() returns a value that is 12 hours earlier than it should be.

Ex. If the user updates the field at 4:00 PM, the datestamp shows 4:00 AM. I have confirmed that he user's time zone and the org time zone are the same.

Here is my code. The middle line (Product_Link_Complete__c) is the one that returns the incorrect datetime. The lines work as expected.

            if (nq.Product_Complete__c != oq.Product_Complete__c) nq.Product_Completed_dt__c = System.Datetime.now();
            if (nq.Product_Link_Complete__c != oq.Product_Link_Complete__c) nq.Product_Link_Complete_dt__c = System.Datetime.now();
            if (nq.Product_Peer_Review_Complete__c != oq.Product_Peer_Review_Complete__c) nq.Product_Peer_Review_Complete_dt__c = System.Datetime.now();

Thoughts? TIA,
Steven NsubugaSteven Nsubuga
datetime.now() shows time in GMT. Use the format(String dateFormatString, String timezone) method to return the appropriate time zone time
You have to use System.Datetime.now().format. Check this for output - http://www.infallibletechie.com/2018/06/how-to-get-current-datetime-as-per.html