learntogrow-growtolearn
learntogrow-growtolearn

Reputation: 1280

Get time difference between two different dates with timestamp?

I have four fields in my HTML. Two are datepicker fields (one for from-date the other for the to-date). Similarly, the other two are timepicker fields (one for the from-time and other for the to-time) .

<input type="text" id="from-datepicker"/>
    <input class="timepicker" name="timepicker" id="from-timepicker"/>
<input type="text" id="to-datepicker"/>
    <input class="timepicker" name="timepicker" id="to-timepicker"/>

The dates are displayed and used in yyyy-mm-dd format, while the time format is 24-hour clock like 23:45:52. Now, I just want to know how can I calculate the time difference between two timestamps such that the difference between 20 Oct 2015 11:00:00 and 28 Oct 2015 13:15:00shall return 7 days 2 hours and 15 minutes

I know how can I get the difference between two dates in JavaScript, but is there any library that provides differences using the timestamp?

Upvotes: 0

Views: 844

Answers (2)

user6307744
user6307744

Reputation:

Here is a function I use. Just modify the output to remove the "ago" part. Maybe change client_time and server_time to be more descriptive of your particular use.

And actually maybe add another if/else for your particular need following the general format.

Pub.prettyTime = function (server_time) {
    var MINUTE = 60,        // 000060 seconds in a minute
        HOUR = 3600,        // 003600 seconds in an hour
        DAY = 43200,        // 43,200 seconds in a day
        NORMALIZE = 1000,   // 00.001 seconds in a millisecond, flipped due to the .1 inaccuracy rule

        // Date.now() is in milliseconds so divide by 1000, to get client_time in seconds
        // this way client time and server time have the same units for comparison
        // this is UTC time
        client_time = Math.round(Date.now() / NORMALIZE),
        rounded_time,
        elapsed_time,
        string = '';

    // here we ensure we never get a negative elapsed time
    // because clients are not synched to the server
    // in the case of negative elapsed time, the server is ahead of the client
    // and we will jus say "just a second ago"
    if (client_time < server_time) {
        client_time = server_time;
    }
    elapsed_time = (client_time - server_time);

    // create the output string
    if (elapsed_time === 0) {
        string = ' just a second ago';

    // 0 to 1 minute ago
    } else if ((elapsed_time > 0) && (elapsed_time < MINUTE)) {
        string = (elapsed_time === 1) ? 'one second ago' :
                (elapsed_time + ' seconds ago');

    // 1 minute to 1 hour ago
    } else if ((elapsed_time >= MINUTE) && (elapsed_time < HOUR)) {
        rounded_time = Math.floor(elapsed_time / MINUTE);
        string = (rounded_time === 1) ? 'one minute ago' :
                (rounded_time + ' minutes ago');

    // 1 hour to to 1 day ago
    } else if ((elapsed_time >= HOUR) && (elapsed_time < DAY)) {
        rounded_time = Math.floor(elapsed_time / HOUR);
        string = (rounded_time === 1) ? 'one hour ago' :
                (rounded_time + ' hours ago');

    // more than 1 day ago
    } else if ((elapsed_time >= DAY)) {
        rounded_time = new Date(server_time * NORMALIZE);
        string = 'on ' + rounded_time.toLocaleDateString();
    }
    return string;
};

Furthermore you can plug in your format above in your Question into the constructor to obtain the normalized timestamp - var d2 = new Date("28 Oct 2015 13:15:00") and finally apply d2.valueOf() to get the unix timestamp. You can also take the difference of dates ( d2 - d1 ).

Using this info. you should be able to achieve what you need.

Upvotes: 2

Ygalbel
Ygalbel

Reputation: 5519

You can use momentjs.

Check substract part in docs.

Upvotes: 1

Related Questions