I have a html form where one of the input is date time.
I am reading the form inputs using jQuery and sending them to the controller using actionFunction and params
The datetime value comes in the following format: 2015-11-30T16:00 (which is 30 Nov 2015 4.00 PM)
I am doing a simple string manipulation to convert that string into Salesforce Datetime value
String datetimeStr = '2015-11-30T16:00'; String dateTimeArray = datetimeStr.split('T'); String dateArray = dateTimeArray.split('-'); String timeArray = dateTimeArray.split(':'); Datetime myDateTime = Datetime.newInstanceGMT( Integer.valueOf(dateArray), Integer.valueOf(dateArray), Integer.valueOf(dateArray), Integer.valueOf(timeArray), Integer.valueOf(timeArray), 0 );
When I debug the Datetime fields it gives the correct conversion value. Reservation_Time_From__c=2015-11-30 16:00:00
However when the record of the object (for which the form was used) is saved, it gives a completely different time (9.30 PM), even though the date is the same.
I tried another way of converting the JS field to SF Datetime field
Date myDate = Date.newInstance(Integer.valueOf(dateArray), Integer.valueOf(dateArray), Integer.valueOf(dateArray)); Time myTime = Time.newInstance(Integer.valueOf(timeArray), Integer.valueOf(timeArray), 0, 0); Datetime myDateTime = Datetime.newInstance(myDate, myTime);
But it gives the same result.
I checked the Company Information and have set the time zone as per my need: (GMT+05:30) India Standard Time (Asia/Kolkata), the same time zone is set for the System Admin as well. I do not understand what I am missing or what is going wrong. I am pretty sure that the answer is in some locale setting that I have missed. I would appreciate any help or insight in this.
reservation.Reservation_Time_From__c = Utility.getJSToSFDateTime('2015-11-30T15:05');
Where the getJSToSFDateTime() has the same conversion code shown above. And guess what? It worked. The time was 3:05 PM in the field. But when I moved this code in the Apex Class (controller) and ran it from the vf page, it gave incorrect time.
Any idea why it gives different result when ran from Apex class and Anonymous Execute?