Current timestamp in advanced event query

Is there any way to get the current date when writing an advanced event query?

For example, I am making a dashboard that will be used solely for generating for a daily report, and would like an Event List block that displays only events that have taken place within the last 24 hours. I can query against the creationDate property in simple and advanced queries, but querying this with a static value does not seem very useful.

Is there a way to do this? It is unclear to me what scope I have access to when writing these queries, or if I can use string templating. Using new Date() as I might in mongodb does not appear valid.

Hi @Nate_Usher,

You can retrieve the current time with the {{currentDateTime formatStr}} format helper, where the formatStr is in Moment.js format. Keep in mind that the time will update only when the block is updated or rendered.

Please let me know if you have any questions on implementation or configuration. :smile:

Thanks,
Julia

1 Like

Great, thanks for pointing me in the right direction.

For posterity, I used:

creationDate > {{subtract (multiply (currentDateTime ‘X’) 1000) 86400000}}

It seems the platform might be using a version of moment.js older than 2.8.4, as

{{subtract (currentDateTime ‘x’) 86400000}}

(‘x’ being the format string for millisecond timestamp)
crashed the block.

@Nate_Usher you’re correct about your query crashing the block; we saw the errors coming through our tracking software and shortly after you provided steps to reproduce. (Thanks!)

Your assumption as to why the block exploded is incorrect, however, as the platform is using the moment@2.24.0. Long story short, your use of {{currentDateTime}} formatted to milliseconds was causing an infinite loop:

  1. Your desired helper resolved to the current time in milliseconds minus 24 hours.
  2. A request was kicked off to fetch events matching the query.
  3. The result of the query came back, which caused the block to re-render. This in turn caused your query string to re-evaluate, which returned a new value for {{subtract (currentDateTime ‘x’) 86400000}}.
  4. This caused a new network request, and thus the loop begins.

Your workaround masks the issue but doesn’t resolve it as {{currentDateTime}} in seconds format changes 1000 times less frequently than when formatted to milliseconds, and a strong internet connection combined with a lightweight query resolves the loop within a second. If you throttle your internet connection to slow it down, and also keep an eye on your browser’s network requests, you’ll see what I mean (but it won’t be fast enough to crash the block so quickly). You can also see the bug in action as the list refreshes when resizing your dashboard block, as that’s kicking off another new time and thus another call.

We’re working on a fix for this but it will be a while before we can get something live, so in the meantime, I recommend you change your “creationDate is greater than” query to the following helper instead:

{{ formatDate (subtract (currentDateTime 'x') 86400000) 'YYYY-MM-DD[T]HH:mm:[00Z]' }}

Essentially what this is requesting all events from the past 24 hours, rounded down to the nearest minute. We’re getting the timestamp of ‘now minus 24 hours’ and instead passing it up to the server in a different, but still acceptable, format. However, we’re ignoring the current time’s seconds and milliseconds values and are instead forcing “:00” for the seconds and no value for milliseconds. This greatly mitigates the re-fetching issue so that it happens at most once per minute.

Thanks again for the steps to reproduce the issue, and we hope to have a more robust fix in our next software release. Let us know if you have any more questions.

Your suggestion works. I appreciate the promptness and in-depth response. You guys really are developer’s developers.

The work is never done :slight_smile: