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))
You can pass javascript date object to the apex date field..
initialise the js date from the date string and pass the js date variable
var dt = new Date('2015-12-12');
var oppDetails = { Name: 'Test Opp', CloseDate: dt, StageName:'New' };
here's the full code I tested in my dev org which worked..
<apex:page >
<script type="text/javascript">
function createOpp(){
var dt = new Date('2015-12-12');
var oppDetails = { Name: 'Test Opp', CloseDate: dt, StageName:'New' };
var opp = new RemoteObjectModel.Opportunity();
opp.create(oppDetails, function(err) {
if(err) {
console.log(err);
alert(err.message);
}
else {
console.log(opp.get('Id'));
alert('success');
}
});
}
</script>
<apex:remoteObjects jsNamespace="RemoteObjectModel">
<apex:remoteObjectModel name="Opportunity" fields="Id,Name,CloseDate,StageName">
</apex:remoteObjectModel>
</apex:remoteObjects>
<input type="button" onClick="createOpp()" value="Create Opp" />
</apex:page>
Best Answer
Assuming all of your dates will be in
yyyy-mm
format.If you're doing this in a Field Update the following formula should work.
In Apex you could use the following:
You could also look into using
Date.parse(String)
orDate.valueOf(String)
, but both those methods are a bit of a pain to use.