+ Start a Discussion
Radhe Shyam.Radhe Shyam. 

need to Save Date in string format like 2017-09-22(yyyy-mm-dd)

Hi All,

I am using datepicker on vf page where i am picking date and saving this in text field.
this date is being saved like 8/25/2017 (mm-dd-yyyy), but i want to save this like 2017-08-25 to use further in dynamic soql query.

Could any one please help me on this.
-Radhe S
Deepak Pandey 13Deepak Pandey 13
Try this-
datetime thisDT = system.now();
String myDate = thisDT.format('yyyy/mm/dd');
system.debug(myDate);
Akshay_DhimanAkshay_Dhiman
Hi Radhe S,
Follow the below code. ​
Visualforce Code :

<apex:page Controller="TestController" > 
    <apex:form id="form">
        <apex:pageBlock title="Edit Contact" id="pageblock">
			<apex:pageBlockSection columns="1" id="pageblocksection">
                <apex:inputField value="{!act.Name}"/>
                <apex:inputField value="{!act.Type}"/>
                <apex:inputField value="{!act.default_date__c}" id="date"/>
                <apex:inputHidden value="{!act.Date__c}" id="theHiddenInput"/>

            </apex:pageBlockSection>
            <div id="test">
            	<apex:pageBlockButtons >
                <apex:commandButton  value="Insert" onclick="MyjavaFunction('{!$Component.form.pageblock.pageblocksection.date}')"/>
            	
            </apex:pageBlockButtons> 
            </div>
            
		</apex:pageBlock>
		<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="test">
        			<apex:param name="firstParam" assignTo="{!state}" value="" />
    	</apex:actionFunction>
    </apex:form>
    <script type="text/javascript">  
		   function MyjavaFunction(today)
		   {
		   		var entereddate = document.getElementById(today).value;
                var userdate = new Date(entereddate);
               	var userday = userdate.getDate();
                var usermonth = userdate.getMonth() + 1; //January is 0!
                var useryear = userdate.getFullYear();
                if (userday < 10) {
                    userday = '0' + userday;
                }
                if (usermonth < 10) {
                    usermonth = '0' + usermonth;
                }
                var userentereddate = useryear + '/' + usermonth + '/' + userday;
               // alert('---hello people---->>'+userentereddate);
                methodOneInJavascript(userentereddate); 
            }
  	</script>     
</apex:page>



Apex Controller Code: -
 
public with sharing class TestController {
	public date userentereddate {get;set;}
	public string state {get;set;}
	public account act { get; set; }
	public TestController(){
       act=new Account();
    }
	public void methodOne() {
    	system.debug(act);
    	act.Date__c=state; 
    	insert act;
    }
}
Screenshot:
User-added image


Hope it helps you!

Regards,
Akshay
 
Radhe Shyam.Radhe Shyam.
Hi all, thanks for ur help. I find the answer on success community where I posted same question. Warm Regards Radhe Shyam