-2

How to convert datetime field to date field?

Hi All,

      This is custom field of datatype  :                
      LightiningEd__Stage_change_from_Prospecting__c (Date) 

       but Closeddate is in datetime datatype how can i convert into date format

    Global class OpportunityStageChanges implements 
      Database.Batchable<sObject> {


      Global Database.QueryLocator start(Database.BatchableContext BC) {  
String query = 'Select Id,Name,Amount,LightiningEd__Stage_change_from_Prospecting__c,(Select ID, CreatedDate,OldValue, NewValue from Histories)from Opportunity';        
return Database.getQueryLocator(query);

} Global void execute(Database.BatchableContext BC, List scope){

Map opplist = new Map(); for(Opportunity opp : scope) {

for(OpportunityFieldHistory opfh : opp.Histories){

      if(opfh.OldValue == 'prospecting' && opfh.NewValue == 'Closed Won ' || opfh.NewValue == 'Qualification'){



        //how can i conert createddate(datetime) into date only 

         opp.LightiningEd__Stage_change_from_Prospecting__c = opfh.CreatedDate;   //here i get the error 

          System.debug('Date Value' + opp.LightiningEd__Stage_change_from_Prospecting__c);
          opplist.put(opp.id,opp);
          System.debug('list of opps'+opplist);

    }        
}
if(opplist.size()>0){   
     update opplist.values();
 }

}
} Global void finish(Database.BatchableContext BC){ System.debug('Closing the batch job');

} }

Thanks In Advance

1

Please check this code

   DateTime dt = System.now();//replace your close date field here
   Date myDate= dt.date();

Or

    DateTime dT = System.now(); ..replace your close date field here
   Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

and you can also refer below Link:

  https://salesforce.stackexchange.com/questions/72388/how-to-convert-date-time-to-date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy