2

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

enter image description here

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.

enter image description here

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?

  • 1
    I know it sounds silly but did you check the user time zone preference..?? – javanoob Nov 30 '15 at 15:45
  • It does not sounds silly. Turns out I had the time zone preference set for the admin but I did not set the time zone for the guest user for the force.com site from where the form was being filled. Feeling stupid right now. really really stupid. thanks for the heads up. – codeinprogress Dec 1 '15 at 3:32
1

Shouldn't you be using Datetime.newInstance() instead of Datetime.newInstanceGMT()?

The way you have it now, your 16:00 is being interpreted as GMT, so when you see it in the +5.30 timezone, the result is 21:30.

  • I thought the same too and changed it to newInstance (the second code block) but it still did not work. – codeinprogress Nov 30 '15 at 8:41
0

Turns out I had the time zone preference set for the admin but I did not set the time zone for the guest user for the force.com site from where the form was being filled.

  • I am glad it worked for you! – javanoob Dec 1 '15 at 3:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.