Transform absolute time (DateTimeType) to relative time

This stopped working for me when, after upgrading to OH3, I started using the new [profile=“system:timestamp-update”] option for my ‘last updated’ Items, instead of the Rule-based approach I’d used before.

I believe that the issue was caused by the profile-based approach now setting the timestamps with MICROsecond precision, instead of MILLIsecond precision which it had used with the Rule-based approach. The JavaScript Date object couldn’t parse the string as it expects only millisecond precision, so everything turned to ‘Invalid date’.

The simplest solution is to use moment.js to parse the string, since it happily works with microseconds. Slightly modifying @kubawolanin’s solution (actually simplifying it!):

(function(i) {
    /* PASTE moment.min.js HERE */
    return moment(i).fromNow();
})(input)