The datetime in salesforce is always stored in GMT (UTC). The time is corrected to the users time zone when displaying it on the UI. The time is only corrected in the UI, so you need to make the adjustment yourself for formula fields.
Datetime input field cast to another timezone
http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm
You need to adjust for your own time zone in your formula to account for this. In my formula fields, since I am in EST, I have to adjust all the datetimes by 5 hours to make sure I have the correct values. I just do this in a long case statement like below. this formula finds the hour of the day that a case was created, notice I have to adjust each time by 5 hours to make it work
TEXT(CASE(VALUE(LEFT(RIGHT(text(CreatedDate),FIND(" ", TEXT( CreatedDate ))-2),2)),
00,19,
01,20,
02,21,
03,22,
04,23,
05,00,
06,01,
07,02,
08,03,
09,04,
10,05,
11,06,
12,07,
13,08,
14,09,
15,10,
16,11,
17,12,
18,13,
19,14,
20,15,
21,16,
22,17,
23,18,
00))
Here's what a DateTime looks like when blatted out on a page as a string:
(format one)
Fri Nov 30 21:55:38 GMT 2012
You can't just hydrate that string back into a DateTime. You must construct or deserialize it.
Here's what a serialized DateTime looks like:
(format two)
"2014-01-24T16:56:27.044Z"
It's not the same as the above string output. You need something more like this:
public DateTime dateFromUrl() {
String serializedDate = ApexPages.currentPage().getParameters().get('startDate');
return (DateTime)Json.deserialize(serializedDate, DateTime.class);
}
And as a good web citizen, if you're serving up the parameter in a link it should be URL encoded:
https://c.na1.visual.force.com/apex/MyPage?startDate=2014-01-24T16%3A56%3A27.044Z
And you might note that the DateTime.valueOf
method expects yet a different value:
(format three)
yyyy-MM-dd HH:mm:ss
Knowing these, we can ensure that we are creating (and expecting) matching date formats.
Best Answer
When dealing with DateTime values in the Salesforce API they should be sent in ISO 8601 format with the UTC value.
Salesforce will change the value displayed in the UI based on the current users UTC offset.
Assuming your sample data is in the Coordinated Universal Time (UTC) time zone, it would be:
See also: Primitive Data Types - dateTime