I am setting datetime value like this-

visitation.Date_of_Visit__c = system.now(); 

Even though current time is 4:30 AM it is showing 4 AM in Date_of_Visit__c But createdDate time is 4:30 AM which is correct. Can anybody please advise/help on this issue.

  • Is Date_of_Visit__c having datetime datatype
    – Reshma
    Commented Jan 23, 2017 at 10:04
  • yes it's of type datetime. Commented Jan 23, 2017 at 10:08
  • 1
    When you are showing 4:30 or 4 you mean on user interface and not on apex debug logs. What is timezone and locale of user ? Commented Jan 23, 2017 at 10:15
  • Yes in UI. If I can see createdDate as 4:30 AM then Date_of_Visit__c should also be 4:30 AM irrespective of Locale. Commented Jan 23, 2017 at 11:07

1 Answer 1


This is because I was using this field later in my code to set external id -

visitation.External_Id__c = visitation.Contact__c + String.valueOf(Date.valueof(visitation.Date_of_Visit__c));

Though I am converting Date_of_Visit__c value from DateTime to Date I am not storing it back to Date_of_Visit__c field. This is something weird I have observed.

I had to make slight changes in my code as shown below -

visitation.Date_of_Visit__c = DateTime.newInstance( dt.getTime());
DateTime extDate = DateTime.newInstance( dt.getTime());
visitation.External_Id__c = visitation.Contact__c + String.valueOf(Date.valueof(extDate));

Instead of using Date_of_Visit__c, have created one datatime variable and used that while setting external id field.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .