Find your content:

Search form

You are here

Why is Task ActivityDate (Due Date) different when queried using SOQL?

 
Share

When I query a Task's ActivityDate in Apex accessed using JavaScript Remoting, I receive a date that is different from the date displayed in the Salesforce interface.

The query below (with a generic Account Id) results in 2012-09-11 for a specific Task, but the date is displayed as 9/12/2012 when viewing the Task in the Salesforce interface. It seems all ActivityDate fields for all Tasks are different by a day.

SELECT Id, AccountId, Account.Name, Priority, Status, Subject, ActivityDate, Type, 
Owner.Name, Owner.Id, What.Name, WhatId, WhoId, Who.Name, LastModifiedDate FROM Task 
WHERE AccountId IN ('001000000000000')

The time-zone of the User accessing Salesforce through the interface and through SOQL is the same - (GMT-05:00) Central Daylight Time (America/Chicago).

Does anyone have advice on how I can account for this in Apex or JavaScript (without Visualforce; I'm using Remote Actions)?


Attribution to: Matt K

Possible Suggestion/Solution #1

Javascript remoting returns dates in milliseconds since epoch. The javascript date constructor interprets the milliseconds since epoch as a date time in your local time zone, thus the discrepancy. Use the following function to correct the date:

var ONE_MINUTE = 60000;
...
function epochToDate(jsRemotingResult) {
  var epochDate = new Date(jsRemotingResult);
  return new Date(epochDate.getTime() + epochDate.getTimezoneOffset() * ONE_MINUTE);
}

Attribution to: Phil Rymek

Possible Suggestion/Solution #2

Ok - the following is a guess. I haven't had time to check it out, but it could be a good directly for further research.

The ActivityDate field in Salesforce is a Date field, not DateTime - so it should be independent of timezone.

However, the Javascript Date object is equivalent to DateTime. So the question is - how is it being created from the Date object?

Let's assume for a moment that the date Sept 11, 2012 is marshalled into a Java date object of Sept 11, 2012, 0:0:0 GMT (which would be quite reasonable, for example, if a millisecond count was being used in the transfer). Well, if you then view the local equivalent of that date anywhere in the world except for the GMT timezone, I expect you would get a date on the previous day - since GMT always gets the date first.

If I'm right, the getUTC... functions on the retrieved object should give you the correct date.


Attribution to: kibitzer
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1667

My Block Status

My Block Content