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
Balaji B 10Balaji B 10 

search a record by Date

Mahesh DMahesh D
Hi Balaji,

Date Formats and Date Literals:

In a SOQL query you can specify either a particular date or a date literal. A date literal is a fixed expression that represents a relative range of time, such as last month, this week, or next year.

dateTime field values are stored as Coordinated Universal Time (UTC). When a dateTime value is returned in Salesforce, it’s adjusted for the time zone specified in your org preferences. SOQL queries, however, return dateTime field values as UTC values. If you want to process these values in different time zones, your application might need to handle the conversion.

Some of the sample SOQL:
SELECT Id
FROM Account
WHERE CreatedDate > 2005-10-08T01:02:03Z
 
Select CreatedDate, Id, LastModifiedDate 
from Opportunity
where   CreatedDate > 2011-01-01T00:00:00Z and CreatedDate < 2011-12-31T00:00:00Z
 
Date myDate = date.newinstance(2011, 1, 1);
Date myDate2 = date.newinstance(2011, 12, 31);
List<Opportunity> oppLst = [Select o.CreatedDate, o.Id, o.LastModifiedDate  from Opportunity o where   o.CreatedDate >: myDate and o.CreatedDate <:  myDate2 order by  o.LastModifiedDate] ;

Also Please go through the below links:

https://nextgensalesforce.wordpress.com/2015/11/30/dynamic-soql-query-results-in-local-time/

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm

Please do let me know if it helps you.

Regards,
Mahesh

 
JyothsnaJyothsna (Salesforce Developers) 
Hi Balaji,
 
Please check the below sample code.

VisualForce Page:
<apex:page controller="DateRangeCont">
 <apex:form id="dt1">
 <apex:pageBlock >
 <apex:pageBlockSection >
 <apex:pageBlockSectionItem >
 <apex:outputLabel >Start Date</apex:outputLabel>
 <apex:inputfield value="{!a.From_Date__c}"/>
</apex:pageBlockSectionItem>
 <apex:pageBlockSectionItem >
 <apex:outputLabel >End Date</apex:outputLabel>
 <apex:inputField value="{!a.To_Date__c}"/>
 </apex:pageBlockSectionItem>
  
<apex:commandButton value="Go" action="{!go}" reRender="dt"/>
 
</apex:pageBlockSection>
 <apex:pageBlockTable value="{!customer1}" var="ct" id="dt">
<apex:column headerValue="Customer Name">
<apex:inputField value="{!ct.Name}"/>
</apex:column>
<apex:column headerValue="billing city">
  <apex:inputField value="{!ct.Billing_City__c}"/>
  </apex:column>
  <apex:column value="{!ct.createddate}"/> 
  <apex:column headerValue="billing postal code">
  <apex:inputField value="{!ct.Billing_Postal_Code__c}"/>
  </apex:column>
</apex:pageBlockTable>
  
</apex:pageBlock>
 </apex:form>
</apex:page>

Controller:
 
public with sharing class DateRangeCont {

    public list<customer__c> customer1 { get; set; }
    
    public datetime startdate1;
    public datetime enddate1;
   public customer__c a { get; set; } 
    public DateRangeCont(){
     customer1=new list< customer__c>();
    a=new customer__c();
     
    }
    public PageReference go() {
    startdate1=a.From_Date__c;
    enddate1=a.To_Date__c;
    customer1=[select name,Billing_City__c ,Billing_Postal_Code__c,Createddate from customer__c where Createddate>=:startdate1 AND Createddate<=:enddate1];
    return null;
    }


    
}
Screenshot:

User-added image



Hope this helps you!
Best Regards,
Jyothsna
 
Kunal Purohit 4Kunal Purohit 4
What is "a" in a.From_date__c?  is From_date__c is field of record?