+ Start a Discussion
Larry D. DortchLarry D. Dortch 

Is it possible to predefine a Date/Time field with NOW() using Visualforce markup?

Hello Group,

I've been attemtping to prepoulate a Date/Time field with the now() function in a Visualforce page but not having any luck.
I've tried using apex inputField, inputText and just apexInput - all returning their various errors and issues.

Is this not possible to do this with Visualforce markup and a Standard controller or does it require a custom apex controller or using javascript?
Amit Chaudhary 8Amit Chaudhary 8
Try to do same in Apex Controller. If that will not work then please share your code.
Larry D. DortchLarry D. Dortch
I'm trying not to write a custom apex controller because the rest of the page works great with a Standard controller.
I'm just trying to set a Date/Time field with the default value now().

For example code, so far I've used something like this but doesn't work
<apex:inputField value="{!ClientVisit__c.Check_In_Time__c = NOW()}" />
Amit Chaudhary 8Amit Chaudhary 8
Try in Apex Constructor like below

ClientVisit__c.Check_In_Time__c  = System.today();

 
Larry D. DortchLarry D. Dortch
Thanks for your input Amit.
Just to be clear,  woluld I write the Constructor in an Extension controller?
Amit Chaudhary 8Amit Chaudhary 8
Yes,
Gokula KrishnanGokula Krishnan
Hi Larry,

try this,
<apex:outputText value="{0,date,dd/MM/yyyy HH:mm:ss}">
    <apex:param value="{!NOW()}"/>
</apex:outputText>

Thanks,

If it helps you, please mark is as best answer, so it will be helpful for other developers.
Larry D. DortchLarry D. Dortch
Hi Gokula,

Thank you for your suggestion but I'm not clear on how I would Save the param into my Date/Time field = Check_In_Time__c ?
Gokula KrishnanGokula Krishnan
Hi Larry,

You need to use controller, if you're using Standard Controller then use Extension else can use customer controller, 
Check_In_Time__c = System.now(); // returns current GMT Datetime Value

Thanks.
Larry D. DortchLarry D. Dortch
Gokula,  thanks again for getting back with me.
It looks as if the answer to my original question is NO.  Visualforce markup alone can't predefine a Date/Time field with the NOW() function.

Thank you all for your input!
Gokula KrishnanGokula Krishnan
Hi Larry, 

Yes, you're right.

I have a suggestion, 
Use Default Value in DateField, as ""Now()" and try out in VF Page. 
User-added image

Thanks,