Skip to Content
Menu

timeAgo()

Convert a timestamp to relative time.

JavaScript April 17, 2021

Usage

JavaScript
nebula.timeAgo(timestamp)

Parameters

timestamp
(Required) (Date Object or String) In specific timestamp format (See below)
Default: None

Parameter Notes

Pass timestamp in the format Fri Mar 27 21:40:02 +0000 2016

Request or provide clarification »

Examples

JavaScript
let loadedTime = new Date();
jQuery('#example').on('mouseover', function(){
    let timeSinceLoaded = nebula.timeAgo(loadedTime);
    console.log(timeSinceLoaded); //-> 2 minutes ago
});

Additional Notes

This is useful for Twitter posts.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/utilities.js on line 741.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.timeAgo = function(timestamp, raw){ //http://af-design.com/blog/2009/02/10/twitter-like-timestamps/
        if ( !timestamp instanceof Date ){
            nebula.help('Pass date as string in the format: Fri Mar 27 21:40:02 +0000 2016 (Your format: ' + timestamp + ')', '/functions/timeAgo/');
        }
    
        let postDate = new Date(timestamp);
        let currentTime = new Date();
        let diff = Math.floor((currentTime-postDate)/1000);
    
        if ( raw ){
            return diff;
        }
    
        if ( diff <= 1 ){ return 'just now'; }
        if ( diff < 20 ){ return diff + ' seconds ago'; }
        if ( diff < 60 ){ return 'less than a minute ago'; }
        if ( diff <= 90 ){ return '1 minute ago'; }
        if ( diff <= 3540 ){ return Math.round(diff/60) + ' minutes ago'; }
        if ( diff <= 5400 ){ return '1 hour ago'; }
        if ( diff <= 86_400 ){ return Math.round(diff/3600) + ' hours ago'; }
        if ( diff <= 129_600 ){ return '1 day ago'; }
        if ( diff < 604_800 ){ return Math.round(diff/86_400) + ' days ago'; }
        if ( diff <= 777_600 ){ return '1 week ago'; }
    
        return 'on ' + timestamp;
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

    nebula.timeAgo = function(timestamp){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.timeAgo = function(timestamp){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.timeAgo === 'function' ){
            nebula.timeAgo = function(timestamp){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });