So I have a visualforce page displaying a query. But when it is being display on a force.com sites page. The created dates are different.
Visualforce page inside sandbox:
Visualforce page on force.com sites:
Here’s the query to get the items
purchaseRequest = [select id, name, CreatedBy.Name, Selected_Request__c, Ownerid, CreatedDate from Purchase_Request__c where Purchase_Request__c.CreatedDate = TODAY];
As you can see, line item PR-00000016 has a datetime 2/25/2014 8:07am for the visualforce page view inside the sandbox environment. Which is PST at time of creation. But the visualforce page that is being viewed through the force.com site public view is show a datetime 2/25/2014 4:07pm for PR-0000016.
My only conclusion is that the server that host the force.com site for me is in another country and is displaying the date in their time zone. Does anyone have any other idea?
I won’t be able to use this to display for our customers if this is happening. Is there a way to set time zones for force.com sites?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Keep in mind, this could be timezone differences. Most likely when you are logged in, created date is converted to your current user’s timezone, while in sites it’s most likely GMT. Please add your timezone to your create date format and you’ll be able to determine if this is the case.
<apex:outputText value="string: {0,date,yyyy.MM.dd 'at' HH:mm:ss z}"> <apex:param value="{!account.CreatedDate}" /> </apex:outputText>
Link to your simple date format doc
Method 2
Force.com Sites displays times in GMT format only. In order to make Site show as per required format, you will need to add hours and then display which will make Force.com Site show correct time.
For eg: the PR-00000016 has a datetime 2/25/2014 8:07am PST and Force.com Site shows in GMT and you want to show in PST, then easy solution is to amend the difference hours and Site will show in desired date time.
Lets say the difference is of 12 hrs between GMT and PST, then use following code:
<apex:outputText value="string: {0,date,yyyy.MM.dd 'at' HH:mm:ss z}"> <apex:param value="{!account.CreatedDate+1/2}" /> </apex:outputText>
Here 1/2 is half day, so if the difference is 4 hrs then it will be 1/6th of day.
Though not recommended solution, but above is simple workaround to get it working immediately.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0