+ Start a Discussion
SabrentSabrent 

DateTime to Date

When trying to convert the DateTime to Date, I am getting this error -

 

Save error: Invalid type: reqt.Last_Submitted_Date__c   


 

 

List<Requirement__c> daysDiff = new List<Requirement__c>();


daysDiff = [Select CreatedDate, Last_Submitted_Date__c, Date_Last_Approved__c,Last_Rejected_Date__c FROM    Requirement__c WHERE Id IN :Ids];
  
System.debug('TEST----------'+daysDiff.size());
        
       for(Requirement__c reqt:daysDiff)   
      
           {         
        

               reqtrack = new Requirement_Tracking__c();
        
               if (reqt.Last_Submitted_Date__c!=''){
              

               reqtrack.Days_Elapsed__c = ((reqt.Last_Submitted_Date__c) - (DateValue(reqt.CreatedDate)));   
             
          }   
      

}        

 

VJSFDCVJSFDC

hi ,

Try using

 

valueOf ----- Date method. which

Returns a Date that contains the value of the specifiedString.

The String should use the standard date formatvalueOf String s Date“yyyy-MM-dd HH:mm:ss” in the local time zone.

Forexample:string year = '2008';string month = '10';string day = '5';string hour = '12';string minute = '20';string second = '20';string stringDate = year + '-' + month+ '-' + day + ' ' + hour + ':' +minute + ':' + second;Date myDate = date.valueOf(stringDate);

 

 

Hope this helps.

SabrentSabrent

I am really struggling with this, can some one please  help me.

 

error: date arithmetic expressions must use integer or long arguments


for(Requirement__c temp:reqList)
       
         {       
                             
             Datetime dateTimetemp = temp.CreatedDate;
            Date dateTemp = Date.newInstance(dateTimetemp.year(),dateTimetemp.month(),dateTimetemp.day());

    if (temp.somecondition){

            reqtrack.Activity_Type__c = 'Created';
            reqtrack.Days_Elapsed__c = (temp.Last_Submitted_Date__c - dateTemp);
            
             }

}



 

VJSFDCVJSFDC

hi ,

 

Datetime dateTemp  = datetime.valueOf(dateTimetemp );

 

for(Requirement__c temp:reqList)                {                                                 

Datetime dateTimetemp = temp.CreatedDate;           

//Date dateTemp = Date.newInstance(dateTimetemp.year(),dateTimetemp.month(),dateTimetemp.day());

here add ***************** Datetime dateTemp  = datetime.valueOf(dateTimetemp );***********

    if (temp.somecondition){

            reqtrack.Activity_Type__c = 'Created';       

            reqtrack.Days_Elapsed__c = (temp.Last_Submitted_Date__c - dateTemp);                         }
}

 

Hope it helps now.

SabrentSabrent

Thanks for helping mw with this . I appreciate it a lot.

 

I tried as you suggested, getting this error


Save error: Date arithmetic expressions must use Integer or Long arguments  

 

 

	for(Requirement__c temp:reqList)
        
         {		
         	Datetime dateTimetemp = temp.CreatedDate;           		
         	Datetime dateTemp = datetime.valueOf(dateTimetemp);
         	
         	if (temp.somecondition){
            
                reqtrack.Activity_Type__c = 'Created';             	
reqtrack.Days_Elapsed__c = (temp.Last_Submitted_Date__c - dateTemp); }

 




VJSFDCVJSFDC

try this will definelty work:

 

 

Long time1= datetimetemp.getTime();

Long time2 = datetemp.getTime();

 

Long diff =  time1 - time2;

 

Long tempdays =  diff / (1000 * 60 * 60 * 24);

 

tempdays will give u difference between two days.

Please make modification to your script accordingly.this is just a prototype.

Hope this helps :)

 

SabrentSabrent

I appreciate your help alot. Thanks! Shall try this. 

 

I do have a work around for my requirement which is not the best approach but will do the job for now.