0

I have a date field

<div class="slds-form-element">
    <label class="slds-form-element__label">Date of Birth</label>
    <div class="slds-form-element__control">
        <input id="EmpDOBId" type="date" class="slds-input" />
    </div>
</div>

I am getting object.dob__c = 86400000 // millisecond value

i want to populate this in the above filed. I have written the below code.

 document.getElementById('EmpDOBId').innerHTML = new Date(object.dob__c);

or

 document.getElementById('EmpDOBId').value = new Date(object.dob__c); 

but it is not populating the value. can someone help me? thanks in advance.

2
  • What context are we in? Lightning/LEX? SF1? Visualforce? Some other server (i.e. plain HTML)? – sfdcfox Jun 1 '17 at 16:17
  • Visualforce. but i am using SLDS for UI – Rumu Jun 1 '17 at 16:27
1

This is a working example, cannibalize a needed, not sure if there is a better way or not:

<apex:page id="myTestPage">
    <apex:slds />

    <div class="slds-form-element">
        <label class="slds-form-element__label">Date of Birth</label>
        <div class="slds-form-element__control">
            <input id="EmpDOBId" type="date" class="slds-input" />
        </div>
    </div>

    <script>
        document.getElementById('EmpDOBId').value = formatDate(new Date(86400000));

        function formatDate(date) {
            var d = new Date(date),
                    month = '' + (d.getMonth() + 1),
                    day = '' + d.getDate(),
                    year = d.getFullYear();

            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;

            return [year, month, day].join('-');
        }
    </script>
</apex:page>

You need to set the value of the input not the innerHTML. The format of the value is yyyy-mm-dd

I do not believe there is a native formater for the javascript date object but I am no Js expert either

Adapted from answer Format JavaScript Date to yyyy-mm-dd

0

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.