You need to sign in to do that
Don't have an account?

Compare createdDate with todays Date
Hi All,
I want to compare createdDate with todays Date.
I have tried this one:
I want to compare createdDate with todays Date.
I have tried this one:
List<contact> con = [Select Id,LastName,Owner.Email,OwnerId,CreatedDate,Email from Contact]; for(contact c : con) { if(c.CreatedDate == system.now()){ system.debug('if'); }else{ system.debug('else'); } }
It is printed else .I want to get IF part.
Help to get the way...............
Thanks in Advance..............!
I have find solution for this.
First we need to take date conversion of both and then use boolean function like this[boolean cmpdt = todayDate_instance.isSameDay(createdDateField_instance);]
Regards,
Harsh P.
All Answers
Created Date and System.Now() are DateTime Stamps. Contacts would have created in past and System.Now will never be equal to contacts created data as it may differ in seconds also.
You need to convert to DateTime to Date format and compare.
Corrected your code-
List<contact> con = [Select Id,LastName,Owner.Email,OwnerId,CreatedDate,Email from Contact];
for(contact c : con) {
if(c.CreatedDate.date() == system.Today()){
system.debug('if');
}else{
system.debug('else');
}
}
Thanks,
Raji M
Thanks for the helpful reply but it also not execute IF statement.
I have find solution for this.
First we need to take date conversion of both and then use boolean function like this[boolean cmpdt = todayDate_instance.isSameDay(createdDateField_instance);]
Regards,
Harsh P.