6

I've been rewriting a Visualforce page to use SLDS styling. Part of this requires rewriting dynamics DateTIme fields to SLDS Date time picker. This therefore means writing the Date part of the DateTime field to the Date input field and the Time part of the DateTime field to the Time input field. What I have done and it works ok is the following.

I've created a hidden Apex:InputField to bind the original Visualforce field to the controller without changing any Apex. When my page loads I read the value of each hidden Apex:InputField field and write the date and time parts to HTML input fields for my Date and Time SLDS pickers.

When the Date is changed via the date picker (or manually) I Rewrite the Date/Time value to hidden Apex:InputField and similarly when the time input changes I rewrite the Date/Time value again also to hidden Apex:InputField. This all binds back to the controller ok and works well. There can be many dynamic Apex:Input fields on the page in my Apex:Iteration but this all works ok. One issue which has arisen though and I've been trying to determine a solution. If the locale changes (I developed this in en_US), the format of how I write my Date/Time to my hidden Apex:Inout field causes issues. If you look at following the Date and time Format's listed must be respected and the value written to the Apex:Input field must respect this.

https://help.salesforce.com/articleView?id=admin_supported_date_time_format.htm&type=5

However while I know of some locale's that will be used I'd prefer not to have to manually check for a few of these in my code and alter how this is written accordingly. I presumed I could use supported dateTime formats but I cannot get any of them to work. I've tried:

YYYY-MM-DDThh:mm:ss+hh:mm YYYY-MM-DDThh:mm:ss-hh:mm YYYY-MM-DDThh:mm:ssZ

I've been using moment js and tried to use it for formatting https://momentjs.com/

I've tried Javascript toUTCString(), toISOString() etc but unless I write to my Apex:Inout field exactly in the format defined in Salesforce Supported Date and Time Formats when I have the locale set for my user it won't work.

IN summary of all this, Is there no standard format I can use for my Apex:Input field that will work regardless of the locale I'm in?

0

While I'm sure there's a way to do this in Javascript so it will bind correctly using the apex:inputField tag, it's going to be a lot easier to pass the date value back to your controller (via an apex:inputText field) and handle the processing there.

In order to make this location-agnostic (for the moment), instead of writing a DateTime value to the hidden apex:inputText field, convert the DateTime to Epoch time -- the number of seconds between now and the Unix Epoch (1/1/1970) -- and store that instead.

In your javascript:

var epochDate = Date.parse(selectedDateValue);

In your apex controller, use the epochDate value to get the current date. Since you're running in user context, the date will be returned in the proper format for the current user's locale setting.

epochDate {get; set;}
...
...

// convert the epochDate string to an integer
Integer numSeconds = Integer.valueOf(epochDate); 

// Get the current date in GMT by adding the epochDate to 1/1/1970
DateTime unixEpochDate = Datetime.newInstanceGmt(1970, 1, 1, 0, 0, 0);
DateTime currentDateGmt = unixEpochDate.addSeconds(numSeconds);
DateTime dateFormatted = currentDateGmt.format();

...and that should do it!

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.