9

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.

3 Answers 3

8

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.

2

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.

0

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

5
  • I've got a Date rather than a DateTime so those methods aren't directly available. Are you suggesting I convert the Date to a DateTime first? Will this take into account the current users timezone? Aug 13, 2012 at 2:10
  • 1
    Ahh I misread that. I'm not sure how a date primitive is stored - my guess is if you have date and convert to datetime it assumes 0:0:00. Someone I found on the boards (haven't confirmed this yet) is "The data in SFDC backend is stored as per organisation timezone." So regardless if the user is in MST if your org is set to Central the date entered is stored as a Central viewpoint. At least that's how I would interpret this. Aug 13, 2012 at 2:19
  • Interesting, because I've been seeing issues where a user in the (GMT +01:00) British Summer Time (Europe/London) time zone is ending up with dates that are ahead by one day compared to the majority of users who are in US time zones. Aug 13, 2012 at 2:34
  • I wonder if the date field is completely timezone agnostic. Seems weird for something such as the activity date though Aug 13, 2012 at 3:43
  • I wasn't so sure there for a second. I thought it was just Date rather than DateTime based on the Partner API. But under Customize > Activities > Task Fields it is "Date/Time". According to Task online docs it is just a Date. Aug 13, 2012 at 4:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .