I have a html form where one of the input is date time.
<input type="datetime-local"/>
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[0].split('-');
String[] timeArray = dateTimeArray[1].split(':');
Datetime myDateTime = Datetime.newInstanceGMT(
Integer.valueOf(dateArray[0]),
Integer.valueOf(dateArray[1]),
Integer.valueOf(dateArray[2]),
Integer.valueOf(timeArray[0]),
Integer.valueOf(timeArray[1]),
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[0]), Integer.valueOf(dateArray[1]), Integer.valueOf(dateArray[2]));
Time myTime = Time.newInstance(Integer.valueOf(timeArray[0]), Integer.valueOf(timeArray[1]), 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.
Thank you.
UPDATE:
I tried inserting the object from anonymous execute with the DateTime provided as a string in javascript format.
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?