8

I have a custom field of type datetime on a custom object. I want to show this field as it is regardless of timezone. As of now, salesforce converts value to user's current timezone while showing on VF page or on standard salesforce pages. Is there any way to avoid that?

Suppose that I am in T1 timezone and I create a record in which datetime field value is 3/10/2013 8:30AM. Now another user from T2 timezone logs in and views the same record. Will he see it as 3/10/2013 8:30AM or conversion will be applied. I want this field to be shown 3/10/2013 8:30AM regardless of user's timezone.

5

The datetime will be converted to the logged-in users local timezone if you use apex:outputField to render your date.

You have to use apex:outputText if you don't want any conversion, which means it will be shown in GMT.

It seems you want to convert the datetime to the record creator's timezone for other users? I am not sure why you would want to do that, but if yes you will have to use Apex to get the timezone of the record creator and convert that before displaying to other users.

3
<apex:outputText value=" {0,date,MM/dd/yy HH:mm a}">
       <apex:param value="<DateTime field>"/>
</apex:outputText>

This will display the date time value in GMT. For more reference you can look at the link : http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_outputText.htm

  • I have updated question. Please see. – doga Mar 11 '13 at 7:28
  • agree with @swatkat. You may have to switch to GMT and display all in GMT format than locale of the user and if you need to convert it then the moment it was created you need to stamp the current user time zone on the record and then probably an utility method to display dates as per created date time zone .painful for sure!!! – Mohith Shrivastava Mar 11 '13 at 8:45
2

How about using a formula field and the day/month/year formula functions to extract the precise format you want.

Then this can be displayed on standard layouts, visualforce, etc.

  • suppose that I am in T1 timezone and I create a record in which datetime field value is 3/10/2013 8:30AM. Now now another user from T2 timezone logs in and views the same record. Will he see it as 3/10/2013 8:30AM or conversion will be applied. I want this field to be shown 3/10/2013 8:30AM regardless of user's timezone. – doga Mar 11 '13 at 7:25
  • I have also updated my question. Please see. – doga Mar 11 '13 at 7:28
2

One more possible/alternative solution.

Create a custom text field that will store the formatted Date and Time in the creators timezone at the point the record is created.

Then use a before insert trigger to explicitly set this custom field with the correct value using the current users (creators) timezone.

If you do go this way, I'd also append the name of the Timezone to the field so users could tell it isn't in their timezone as they might otherwise expect with Salesforce.

Q: Why go to all the effort of creating a trigger and a customer field? (and some test cases)

A: The custom field won't be affected by changes to the creators time zone and you don't need to lookup the creators timezone to display the value.

E.g. The user who created the record may be in T1 today, but they could easily be in T3 tomorrow and T4 a year from now. I guess the point is a users timezone is in no way fixed.

  • What impact does this have on the performance of SOQL queries that need to get records based on this text version of the datetime? – Nick Cook Aug 9 '16 at 23:52
  • @NickCook I haven't checked, but my guess would be that the field being indexed or not will have the biggest impact. As long as it isn't a Long Text Area there shouldn't be a significant difference other than not being able to use the Date functions in the where clause. Asking a more general question of "Is a SOQL query filtering by a text field or a DateTime field more efficent?" could be interesting. – Daniel Ballinger Aug 10 '16 at 2:40
0

Why doesn't Salesforce do what Siebel does and allow the developer to decide if he wants to use a timezone enabled datetime field or a non-timezone enabled datetime field. That way if you always want to see the datetime value as it was entered whatever your timezone setting happens to be you could define a new non-timezone enabled datetime field.

  • 2
    This is not really an answer to the question, is it ? If not, please use the comment feature. – Samuel De Rycke Jun 12 '13 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.