1

I am needing some help with a query that contains a date. I am collecting this date through a VF page with the help of a birthday fake contact field, my class is something like this: Controller:

public with sharing class MyCLass {

public Contact fakeCont { get; set; }
public String queryString { get; set; }
public MyCLass() {
    fakeCont = new Contact();       
}

public void doQuery(){
    queryString = 'SELECT id, Name FROM SObject WHERE dateExample = ' + fakeCont.Birthdate  ; 
}

}

VF Page:

<apex:inputField styleClass="form-control" value="{!fakeCont.Birthdate}"/>

even that the field in contact is mark as date after selecting it displays something like 2014-11-02 00:00:00

it is not supposed to be displayed like: 2014-11-02 ? as this field is date and not datetime?

if I try to use something like

newDate = fakeCont.Birthdate.date();

I will get an error

1

If you bind the date field into the query rather than make it part of the query string you will avoid this problem. So your query string becomes:

 queryString = 'SELECT id, Name FROM SObject WHERE dateExample = :fakeCont.Birthdate';

Note that generally its better not to use strings in queries but instead make use of the compile-time checked static form:

Xyz__c[] sobs = [SELECT id, Name FROM Xyz__c WHERE DateField__c = :fakeCont.Birthdate];
  • Thank you, I also that I have another issue my field is dd/mm/yyyy and this one is yyyy-mm-dd, the only thing that I have found online to change the format is using something like: dateInitial.format('dd/MM/yyyy') however this seems to only works with datetime, how can i change this format when it is only date? Thanks – manza Nov 30 '14 at 23:14
  • will be better to do it from the VF Page? curretnly I have <apex:inputField styleClass="form-control" value="{!fakeCont.Birthdate}"/> – manza Nov 30 '14 at 23:15
  • ; actually I just realie that in the VF Page it displays correctly the problem is in the controller that it swaps the format – manza Nov 30 '14 at 23:25
0

If you are going to use a String to show the date use this:

String dateToShow = CustomObject.BdayDate__c.format();

It will take out the time (00:00:00);

Best regards!

0

You can use this to show a date in your format

<apex:outputText value="{0,date,yyyy'-'MM'-'dd}">
    <apex:param value="{!fakeCont.Birthdate}" /> 
</apex:outputText>

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.