Which function returns the time difference in milliseconds in javascript?

moment[].diff[Moment|String|Number|Date|Array]; moment[].diff[Moment|String|Number|Date|Array, String]; moment[].diff[Moment|String|Number|Date|Array, String, Boolean];

To get the difference in milliseconds, use moment#diff like you would use moment#from.

var a = moment[[2007, 0, 29]];
var b = moment[[2007, 0, 28]];
a.diff[b] // 86400000

To get the difference in another unit of measurement, pass that measurement as the second argument.

var a = moment[[2007, 0, 29]];
var b = moment[[2007, 0, 28]];
a.diff[b, 'days'] // 1

To get the duration of a difference between two moments, you can pass diff as an argument into moment#duration. See the docs on moment#duration for more info.

The supported measurements are years, months, weeks, days, hours, minutes, and seconds. For ease of development, the singular forms are supported as of 2.0.0. Units of measurement other than milliseconds are available in version 1.1.1.

By default, moment#diff will truncate the result to zero decimal places, returning an integer. If you want a floating point number, pass true as the third argument. Before 2.0.0, moment#diff returned a number rounded to the nearest integer, not a truncated number.

var a = moment[[2008, 9]];
var b = moment[[2007, 0]];
a.diff[b, 'years'];       // 1
a.diff[b, 'years', true]; // 1.75

If the moment is earlier than the moment you are passing to moment.fn.diff, the return value will be negative.

var a = moment[];
var b = moment[].add[1, 'seconds'];
a.diff[b] // -1000
b.diff[a] // 1000

An easy way to think of this is by replacing .diff[ with a minus operator.

          // a < b
a.diff[b] // a - b < 0
b.diff[a] // b - a > 0

Month and year diffs

moment#diff has some special handling for month and year diffs. It is optimized to ensure that two months with the same date are always a whole number apart.

So Jan 15 to Feb 15 should be exactly 1 month.

Feb 28 to Mar 28 should be exactly 1 month.

Feb 28 2011 to Feb 28 2012 should be exactly 1 year.

See more discussion on the month and year diffs here

This change to month and year diffs was made in 2.0.0. As of version 2.9.0 diff also support quarter unit.

In this tutorial, I will guide you on how to write a function that calculates the time difference between two JavaScript Date objects and return the calculation of the difference in days, hours, and minutes.

You can get the javascript difference between date objects by subtracting the date Objects. It will return the difference in milliseconds.

const diffInMilliseconds = Math.abs[new Date['2019/10/1 00:00:00'] - new Date['2019/10/2 00:00:00']];

console.log[diffInMilliseconds]; //86400000

In the above example, I have subtracted two date objects which returns the milliseconds. Math.abs is used to get positive numbers. The above example return 86500000 [milliseconds] which is exactly 1 day.

Calculate Time Difference between two dates in JavaScript

function timeDiffCalc[dateFuture, dateNow] {
    let diffInMilliSeconds = Math.abs[dateFuture - dateNow] / 1000;

    // calculate days
    const days = Math.floor[diffInMilliSeconds / 86400];
    diffInMilliSeconds -= days * 86400;
    console.log['calculated days', days];

    // calculate hours
    const hours = Math.floor[diffInMilliSeconds / 3600] % 24;
    diffInMilliSeconds -= hours * 3600;
    console.log['calculated hours', hours];

    // calculate minutes
    const minutes = Math.floor[diffInMilliSeconds / 60] % 60;
    diffInMilliSeconds -= minutes * 60;
    console.log['minutes', minutes];

    let difference = '';
    if [days > 0] {
      difference += [days === 1] ? `${days} day, ` : `${days} days, `;
    }

    difference += [hours === 0 || hours === 1] ? `${hours} hour, ` : `${hours} hours, `;

    difference += [minutes === 0 || hours === 1] ? `${minutes} minutes` : `${minutes} minutes`; 

    return difference;
  }

  console.log[timeDiffCalc[new Date['2019/10/1 04:10:00'], new Date['2019/10/2 18:20:00']]];

// the time difference is:
// 1 day, 14 hours, 10 minutes

The above function will work perfectly to calculate the time difference [except daylight saving] between two dates.

The first line of the function is to get the time difference in milliseconds by subtracting two Date objects. From that milliseconds, we can calculate days, hours, and minutes. I will explain it step by step below.

Calculate The Number of Days Between Two Dates in JavaScript

// calculate [and subtract] whole days
const days = Math.floor[diffInMilliSeconds / 86400];
diffInMilliSeconds -= days * 86400;
console.log['calculated days', days];

To calculate the number of days difference between two objects, we should divide the diffInMilliSeconds [time difference in milliseconds] with 86400 [which is the number of seconds per day].

After that, we should subtract the days in diffInMilliSeconds with the number of days in milliseconds [days * 86400] for further accurate calculation of hours and minutes.

Calculate Hours Difference between Two Dates in JavaScript

// calculate hours
const hours = Math.floor[diffInMilliSeconds / 3600] % 24;
diffInMilliSeconds -= hours * 3600;
console.log['calculated hours', hours];

To calculate the hours difference of the two dates, we should divide the diffInMilliSeconds with 3600 which is the milliseconds of 1 hour, and then further divide it by 24 [24 hours per day] and get the remainder using modules operator. After that, subtract the diffInMilliSeconds with the number of hours in milliseconds [hours * 3600] for further calculation of minutes.

Calculate Minutes Difference between Two Date in JavaScript

// calculate minutes
const minutes = Math.floor[diffInMilliSeconds / 60] % 60;
diffInMilliSeconds -= minutes * 60;
console.log['minutes', minutes];

To calculate the minutes difference between two dates, we should divide the diffInMilliSeconds with 60 seconds] and then get the remainder by dividing it by 60 [60 minutes per hour]. After that, subtract the diffInMilliSeconds with the number of minutes in milliseconds [minutes * 60].

Conclusion

I hope, you have learned to play with the Date to calculate the time difference in JavaScript. The above method will work for most of the situations. If you have to manage the daylight saving in your calculation this method is not recommended. I highly recommend you to use countdown.js for overcoming the daylight saving issue. Thank you.

Nithi is a professional developer experienced in Web and Android development. He is leading a front-end team in Accubits Technologies. He loves to share his experience in blog posts to help other developers across the globe.

Which function returns the time difference in milliseconds?

You can use DiffSeconds[] built-in function and multiply the result by 1000. The output will be milliseconds.

Which method returns the number of milliseconds?

getTime[] returns the number of milliseconds since January 1, 1970 00:00:00.

How do I get the difference between two time in JavaScript?

Live Demo:.
function diff_minutes[dt2, dt1].
var diff =[dt2. getTime[] - dt1. getTime[]] / 1000;.
diff /= 60;.
return Math. abs[Math. round[diff]];.

How do you find the difference between two dates in milliseconds?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day [1000*60*60*24]

Bài mới nhất

Chủ Đề