Thursday, January 12, 2017

What is Default Backend formats for Date and DateTime in Salesforce

Today I'm going to share an exiting hidden feature of salesforce. We will be using regularly date and datetime fields in salesforce. I will discuss few points about these fields now.

Scenario:

 I have one datetime field in my object which typically stores the date time .Now I have created one record in that object passing some date and time to that field.Lets assume that the value is 11 Jan 2017 and time as 3Am(03:00:00) .I have successfully created a record and my time zone is IST .
Now I open my developer console and trying to query the same record with the help of datetime(11Jan 2016 3Am) then my query ends up with no result.Why is it ?What happened?

Root Cause:

 Now I have taken some other field(Id or String) from same object to debug actually what happening in the backend and included this datetime field in query.The value for datetime field is showing some thing like 10 Jan 2017 and time as 9pm (21:30:00) but when I added same field to page layout or in visual force page I'm getting the 11 Jan 2017 and time as 3Am(03:00:00) .Then I went through many blogs and docs of salesforce and came to know that by default the Salesforce will stores all Date and Datetime fields in database in the format of Coordinated Universal Time (UTC) which also know as Greenwich Mean Time (GMT) and it will not consider what is the logged in user time zone.



Solution:

So due to above mentioned statement the converted value of IST 11 Jan 2017 and time as 3Am(03:00:00) will comes as 10 Jan 2017 and time as 9pm (21:30:00) because we are ahead of +5:30 compared to GMT/UTC.

So when you were working with date or datetime fields better you will make sure that you follow all these conversions to get the exact data.

Whenever showing these date or datetime fields in pagelayout or visual force page salesforce automatically converts that GMT/UTC value to logged in user respective timezone so that is the reason we are able to see 11 Jan 2017 and time as 3Am(03:00:00) on layouts and vf pages instead of 10 Jan 2017 and time as 9pm (21:30:00)

Salesforce already provides some internal date and datetime functions to make all these conversions.
We will see more details about all these functions in my next post.

Thanks for visiting..hope this helps you!









No comments: