+ Start a Discussion
Danny LeungDanny Leung 

Use datevalue() in VisualForce page

Hi,

 

I got the error 'Invalid argument type for function datevalue()' when I include the markup tag <apex:column value="{!datevalue(mt.createdDate)}"/>  where mt is a custom object.

 

To my understanding, createdDate is a system datetime field.  Do anyone know why it is an invalid argument type for datevalue()?

 

Thanks

Danny

bob_buzzardbob_buzzard

I've had problems with this in the past.  In my case, although the createddate is treated as a datetime in Apex, when I accessed in in a VisualForce page I had to treat it as a date object.

 

Try <apex:column value="{!DAY(mt.createdDate)}"/> 

 

and see what happens.  If its saves correctly you are seeing the same thing that I did.

Danny LeungDanny Leung

Hi, Bob

 

Thanks for your advice.  I tried it as you suggested.  I could save the VF page without error.  However, when I executed the VF page, I got an internal server error:

 

An internal server error has occurred

An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.

Thank you again for your patience and assistance. And thanks for using salesforce.com!

Error ID: 2036083350-1142 (-1809644740)

Regards

Danny

bob_buzzardbob_buzzard

It looks like you are seeing something similar to me, albeit with a different outcome.

 

I ended up adding a getter to my controller that retrieved the formatted date as a string.

SSRS2SSRS2

DateValue() need date/time or text as parameter therefore.

Try with 

 

<apex:column value="{!datevalue(Text(mt.createdDate))}"/>

-Suresh

 

aballardaballard

What is the api version setting for your page?  There was a bug in previous releases that caused confusion between date and datetime in some situations but it should be fixed for pages that have version set to 20. 

Danny LeungDanny Leung

I upgraded my page to version 20 and tried the method suggested by Suresh.  Unfortunately, I still got the same error.

aballardaballard

There is definitely something wrong here.   With version=20  and the syntax

<apex:column value="{!datevalue(r.createdDate)}"/>

it saves ok but gives me a spurious runtime errror "syntax error:  invalid ) " (or something like that)

 

Problem seems to be specific to apex:column... works ok in outputText and some other components I tried.

 

As a possible workaround, you might try

 

<apex:column >
  {!datevalue(r.createdDate)}
</apex:column>

 

which works for me (where r is a reference to the custom object). 

Danny LeungDanny Leung

I tried your suggestion and the VF page could save and run without error.  However, it didn't display the date in the format I need.

 

If I use <apex:column value={!mt.createdDate}/>, it displays something like "28/10/2010 4:51 PM".

 

If I use <apex:column>{!datevalue(mt.createdDate)}</apex:column>, it displays "Thu Oct 28 00:00:00 GMT 2010".

 

What I want is simply something like "28/10/2010".

aballardaballard

You can use <apex:outputText> together with a format specification to format the date the way you want (there is an example of date formatting in the VF reference doc for outputText

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_compref_outputText.htm

 

 

But note that it is going to be based on the raw value which is GMT based, so might be out by one day from what you get in the original version (the column value attribute, like outputField, uses standard VF formatting/locale/etc.)