Long startTime = 1112222220;
Date startDate  = DateTime.newInstance(Long.valueOf(startTime)).dateGmt();
system.debug('startDate: '+startDate);
String qry = 'Select Id, Subject from Task where ActivityDate > :startDate';
system.debug('qry: '+qry.replace(':startDate', ''+startDate));

The above prints the debug as:

startDate: 1973-07-11 00:00:00

qry: Select Id, Subject from Task where ActivityDate > 1973-07-11 00:00:00

It seems to contain the time portion and I do not get any Task records as part of my query. SO as I understand, I got a Date variable and I also extracted correct Date and assigned it. Then used it in a SOQL variable appropriately, but still something is not right, I might be ignoring something small, but impactful in my understanding, so need some guidance what might be wrong here in my understanding.

If I remove the time portion from startDate variable manually, my Query returns appropriate number of Task records.

  • Do you want to put just date in the query like 1973-07-11?? – rahul gawale May 7 at 10:11
  • 1
    yes that is right. I actually expected date variable to NOT contain anything for a time portion at all, doesnt make sense to me, why it would do. – VarunC May 7 at 10:41
  • Actually the date always contains time but its fixed to 00:00:00. – rahul gawale May 7 at 11:43
  • try this String strdate = DateTime.newInstance(startTime ).format('yyyy-MM-dd'); – rahul gawale May 7 at 12:06

dateGmt() returns the Date component of a Datetime in the GMT time zone.

What you are looking for is format(). You need to format your date literal to a SOQL expected syntax.





| improve this answer | |
  • That is right. I know but I am looking to understand why even after assigning to a DATE type variable and returning date only from function dateGmt() would still contain a Time portion. It does not make sense to me. – VarunC May 7 at 10:42
  • Date class in SF contains the time portion as well but it is always points to midnight since midnight is the start of the day. dateGMT, date and , Date.newInstance from the structure perspective are the same. DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 23, 0, 0); Date myDate = myDateTime.dateGMT(); Date expected = Date.newInstance(2006, 3, 17); System.debug('Dates'+myDate+'=='+expected); System.assertEquals(expected, myDate); – Mert YALTI May 7 at 13:52

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.