1

I want to query last 24 hrs of Date field in my custom object. But when I debug the value, they become like that.

Example : Date.today() : 2013-10-04 00:00:00

last24hrs : 2013-10-03 00:00:00

So totally,no current time of query made is used. Now it is like I am querying for since yesterday results. Date.today().addDays(-1);

My requirement is using hour,min,second too. But the custom field is only Date. Example if I make query at 2013/10/04 13:00 , then the result of since 2013/10/03 13:00 should appear. before 13:00 hr results should not come out.

How should I approach this issue?

9

So you want to be able to query an object where a date has a date time value within the last 24 hours?

I think that you need to change your field from date to datetime if you want to get that data.

It's hard to tell exactly what you are doing wihout seeing your code but maybe try using system.now() instead of date.today() // OR datetime.now() either way

Run this in execute anon to see what I mean

  Date TodayDate = date.today();
  DateTime NowDate = datetime.now();
  DateTime Past24 = system.now().AddDays(-1);

  system.debug('OriginalDate::' + TodayDate);
  system.debug('NowDate::' + NowDate);
  system.debug('Past24::' + Past24);

If that doesn't help please post your code.

| improve this answer | |
  • I will accept it as an answer. As all the answers have mentioned for Date value field can't get DateTime value. Only approach is to change the field type itself. For me, I was so lucky to find another field that is DateTime field type that can used as alternative. Otherwise, I might need to change the field type itself. – kitokid Oct 8 '13 at 10:03
5

A Date value has no time component, while a DateTime value does. Thus, for fields that are only a date, you can only query with an accuracy of 24 hours, because date fields do not store a time. You can tell if a date was "today" or "yesterday", but you cannot tell if it was "yesterday after 4pm", because the time isn't stored. You may need to change your field from a Date field to a Date/Time field, if possible.

| improve this answer | |
0

I suggest use System methods because they work faster! The difference is quit small, until you are calling these methods millions of times: For Date.today(), the cost per invocation is about 4.43µs, whereas System.today() costs about 3.85µs per invocation, a difference of about ~15%. For Datetime.now(), the cost per invocation is about 3.48µs, whereas System.now() costs about 2.97µs per invocation, a difference of ~17%.

| improve this answer | |

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.