Transform absolute time (DateTimeType) to relative time

instead of using the moment.js, i am using the below crudely written JavaScript code to do the same job.
Then using transformation, a string type with a data time stamp is converted to relative time.
Just sharing in case its helpful to anyone.

(function(i) {
    var eTime = "00:00:00";
    var i_date = new Date(i);
    var z_date = new Date ();   
    if (i !== null) {
        var eSecs = (z_date.getTime()-i_date.getTime())/1000;
        // Calculate d/h/m/s without using modulus
        var d = parseInt((eSecs / (60*60*24)));                       // Div by 86400
        var h = parseInt(((eSecs - (d*60*60*24)) / (60*60)));         // Div by 3600
        var m = parseInt(((eSecs - (d*60*60*24) - (h*60*60)) / 60));  // Div by 60
        var s = parseInt((eSecs - (d*60*60*24) - (h*60*60) - (m*60)));         // Remainder = secs
        if (d >=1) { eTime = d+" days "+h+" hours "+m+" minutes Ago"; }
        if (d < 1 && h >=1) { eTime = h+" hours "+m+" minutes Ago"; }
        if (h<1) { eTime= m+" minutes Ago"; }
        }
        return eTime;
})(input) 

You can use d, h, m, s to format output string as you like.
full credits to this code here: Lambda Function to get Elapsed Time

1 Like