Find your content:

Search form

You are here

Date input taking into account users time zone offset

 
Share

I have an input field that is binding to a backing Task in the controller to allow users to enter a date into the ActivityDate field.

I'm not interested in any other fields on the Task. I'm just using it as a mechanism to get the date picker.

Controller:

public Task startDateTask {get; set;}

Page:

<apex:inputField value="{!startDateTask.ActivityDate}"/>

Taking into account the users time zone offset, how can I determine the Date the user entered in UTC/GMT?

The Date primitive data type doesn't appear to have any of the GMT methods that DateTime has. This makes sense, as there is no offset information in just a date without a time.

But when a user that is in a UTC +12 timezone enters the same date as a UTC -10 user they are not talking about the same day of the year. This causes issues when passing the dates off to a web service.


Attribution to: Daniel Ballinger

Possible Suggestion/Solution #1

There are GMT functions you could use:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

check dateGMT... there's also a GMT option for day, hour, minute and second


Attribution to: Salesforce Wizard

Possible Suggestion/Solution #2

Date is timezone non-aware, in fact, so is datetime (its stored at the server as GMT and re-rendered based on users TZ).
Unless you hard-code a base time for your date in Apex code/Trigger, your going to want store a datetime value instead of a date. If you dont want the time part to show up, look into creating a formula field that renders the date based upon the (hidden) datetime field. That should maintain the correct date rendered for their timezone.


Attribution to: ebt

Possible Suggestion/Solution #3

I've tracked my specific issue down. The call to the web service was fine. The issue was how the DateTime values that came back in the response were handled.

The web service would return DateTime values like the following:

<EndDate>2012-07-07T00:00:00</EndDate>

Then the code base had done the following:

Date endDate = date.newinstance(response.EndDate.year(), 
                                response.EndDate.Month(), 
                                response.EndDate.Day()).addDays(1);

Alarm bells started going off at the .addDays(1) (in my defence this was already in the code base before I started on it). When the code was first written it looks like most users where on:

Time Zone (GMT-07:00) Pacific Daylight Time (America/Los_Angeles)

As the web service was returning times at midnight the call to day() would return the day-of-month in the local time zone of the context user. In the example above this ends up being the 6th of July in PDT.


Attribution to: Daniel Ballinger
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/352

My Block Status

My Block Content