[javascript] Check time difference in Javascript

You can Get Two Time Different with this function. 

_x000D_
_x000D_
 /**_x000D_
     * Get Two Time Different_x000D_
     * @param join_x000D_
     * @param lastSeen_x000D_
     * @param now_x000D_
     * @returns {string}_x000D_
     */_x000D_
    function getTimeDiff( join, lastSeen, now = false)_x000D_
    {_x000D_
        let t1 = new Date(join).getTime(), t2 = new Date(lastSeen).getTime(), milliseconds =0, time ='';_x000D_
        if (now) t2 = Date.now();_x000D_
        if( isNaN(t1) || isNaN(t2) ) return '';_x000D_
        if (t1 < t2) milliseconds = t2 - t1; else milliseconds = t1 - t2;_x000D_
        var days = Math.floor(milliseconds / 1000 / 60 / (60 * 24));_x000D_
        var date_diff = new Date( milliseconds );_x000D_
        if (days > 0) time += days + 'd ';_x000D_
        if (date_diff.getHours() > 0) time += date_diff.getHours() + 'h ';_x000D_
        if (date_diff.getMinutes() > 0) time += date_diff.getMinutes() + 'm ';_x000D_
        if (date_diff.getSeconds() > 0) time += date_diff.getSeconds() + 's ';_x000D_
        return time;_x000D_
    }_x000D_
    _x000D_
    _x000D_
    console.log(getTimeDiff(1578852606608, 1579530945513));_x000D_
    
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to date

How do I format {{$timestamp}} as MM/DD/YYYY in Postman? iOS Swift - Get the Current Local Time and Date Timestamp Typescript Date Type? how to convert current date to YYYY-MM-DD format with angular 2 SQL Server date format yyyymmdd Date to milliseconds and back to date in Swift Check if date is a valid one change the date format in laravel view page Moment js get first and last day of current month How can I convert a date into an integer?

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?