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
ShaikAShaikA 

Soql DateTime queried result and actual dateTime showing different

Hi All,

I have two Date/Time fields : StartTime and EndTime, When queried it result is not correct. It is showing diffrent than actual.

Entered Date:
User-added image
Qureid Result Date:
mRoomObj.Start_Date__c>> 2018-10-02 22:00:00
mRoomObj.End_Date__c>> 2018-10-03 00:00:00

Please help me how i can fix this issue

Thanks in Advance
Shaik
NagendraNagendra (Salesforce Developers) 
Hi Shaik,

From documentation:

dateTime field values are stored as Coordinated Universal Time (UTC). When a dateTime value is returned in Salesforce, it’s adjusted for the time zone specified in your org preferences. SOQL queries, however, return dateTime field values as UTC values. If you want to process these values in different time zones, your application might need to handle the conversion.

So it is possible that when you're storing date in Salesforce it's getting converted into UTC format and that's why it is returning in that format only. You can convert that dateTime into different time zones as per your need.

Also, try the below approach:
public Double offset{get{
TimeZone tzone = UserInfo.getTimeZone();
//Milliseconds to Day
return tzone.getOffset(DateTime.now()) / (1000 * 3600 * 24.0);}}
And then we need to add offset to the retrieved values.

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
 
Ajay K DubediAjay K Dubedi
Hi Shaika,

Below Sample code can fulfill your requirements. Hope this will work for you.

Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi