Time Formatting (formatDateRelative})


#1

Hi,

Just a few questions for implementing relative time as a string for dashboard views.

I am trying to use the formatDateRelative formatter, which works but there doesn’t appear to be any documentation for how to format the relative time. Currently, with no format string the relative time between events appear as:

“a few seconds ago”
"in 5 days
“in 10 days”

I would prefer to be able to format the relative time to round to the days and output this just as the decimal string without any additional words.

A secondary issue I am seeing with this formatter is that if we consider the format as:

{{formatDateRelative value relativeTo}}

Then the formatter receives no value the string returns “a few seconds ago” automatically (i.e it takes the current time automatically). Could this possibly be disabled? I imagine for most applications reporting the current time when an attribute isn’t available is undesired behaviour.

Thank you


#2

There is no way to currently hand a format string to formatDateRelative, but in your case there is a way to do what you want, since you always want days. Given your time value residing in the variable yourTimeValue, I think you want something like this:

{{#if yourTimeValue}}
{{format (divide (subtract (currentDateTime 'x') yourTimeValue) 86400000) '0.4'}} days ago
{{else}}
No Time Provided
{{/if}}

If yourTimeValue is set, the above takes the current time (in ms), subtracts yourTimeValue (which can either be a number in ms or a date - the date will be converted to ms), then divides by 86400000 (the number of milliseconds in a day), and formats the resulting number to 4 significant digits. If yourTimeValue is not set, it will print out “No Time Provided”.

Hope that helps!