function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chamil MadusankaChamil Madusanka 

set Date value for apex date field in javascript onload

Hi  All,

 

I trying to set a date value for Apex date field in javascript. How to do that?

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Am supposing that you want to do this by using standardcontroller on visualforce page, try this :

 

<apex:page id="pg" standardController="Account">
<apex:form id="frm">
    <apex:pageBlock id="pb">
        <apex:pageBlockSection id="pbs">
            <apex:pageBlockSectionItem id="pbsi">
                <apex:inputField id="dt" value="{!Account.End_Date__c}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
<script>
    document.getElementById('pg:frm:pb:pbs:pbsi:dt').value = '{!Now()}' ;
</script>
</apex:page>

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

All Answers

kiranmutturukiranmutturu

u have to use the javascript's date object like this

 

var myDate=new Date();..

Ankit AroraAnkit Arora

Chamil, can you please explore more what exactly you want to do? So I can provide you the code.

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

Chamil MadusankaChamil Madusanka

Hi Ankit,

 

I have Date field and I have used in VF page as a <apex:inputField>. I want to set date value on page onload in javascript.

 

SteveBowerSteveBower

If you are using a Custom controller, or a controller extension, then instead of doing it in Javscript, you're probably better served doing it in the Controller Constructor.

 

If you are only using a standard controller, and you are trying to set a field on the form to a specifc data value, then you need to use the $component to get the Id of the Date field, then use standard DOM to set the value for the field with that ID.  However, you might be better off setting a default value for that field in the field definition.

 

So, there are many answers depending on what you are trying to do.   Clarify your business need before digging into a specific implementation question.

 

Best, Steve.

Ankit AroraAnkit Arora

Am supposing that you want to do this by using standardcontroller on visualforce page, try this :

 

<apex:page id="pg" standardController="Account">
<apex:form id="frm">
    <apex:pageBlock id="pb">
        <apex:pageBlockSection id="pbs">
            <apex:pageBlockSectionItem id="pbsi">
                <apex:inputField id="dt" value="{!Account.End_Date__c}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
<script>
    document.getElementById('pg:frm:pb:pbs:pbsi:dt').value = '{!Now()}' ;
</script>
</apex:page>

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

This was selected as the best answer
aballardaballard

Instead of hard-coding the reverse-engineered dom-id as shown you should use {!$Component.dt} to get the dom-id for the component with id='dt'

 

document.getElementById('{!$component.dt}').value



Balakrishna MandulaBalakrishna Mandula

When I click the "Save" button date is inserted in the model. And also I am not getting date picker in visual force page.

 

In Visual force Page

<apex:pageBlockButtons >
         
            <apex:commandButton value="Save" action="{!savetherecord}"/>       

</apex:pageBlockButtons>

 

<apex:outputText >Start Date</apex:outputText>
              <apex:inputText value="{!stdate}"/>

 

 

//Controller

 

public class MyCampaign {

  public Date stdate{get;set;}

   public PageReference savetherecord(){

 

      MyCampaign__c mc = new MyCampaign__c();

      mc.Start_Date__c = stdate;

     PageReference pr = new PageReference('https://ap1.salesforce.com/a0D/o');
     return pr;
    }  

 

 

Could you please hepl me