Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the minutes that have elapsed through moment diff

I'm trying to get the difference in minutes between two timestamps

I have a timestamp that looks like this to begin with

'15:44:06'

And when I want to find the time elapsed I create a new moment timestamp

var currentTimestamp = Moment(new Date()).format('HH:mm:ss');

Which returns this

'15:42:09'

I then attempt to get the difference like so

var duration = Moment.duration(Moment(currentTimestamp,'HH:mm:ss').diff(Moment(userTimestamp,'HH:mm:ss')));

And then attempt to get it in minutes

var elapsedTime = duration().asMinutes();
console.log(elapsedTime);

But in the console when I log it I get this error

var elapsedTime = duration().asMinutes();
                  ^
TypeError: object is not a function

I got most of this code from this stackoverflow

like image 372
Jordan Avatar asked Sep 28 '15 19:09

Jordan


People also ask

How do you find the moment difference in time?

To get the hour difference between two times with moment. js, we can use the duration , diff , asHours and asMinutes methods. For instance, we can write: const startTime = moment("12:26:59 am", "HH:mm:ss a"); const endTime = moment("06:12:07 pm", "HH:mm:ss a"); const duration = moment.

What is moment diff?

The moment(). diff() function is used to get the difference in milliseconds of given dates which are passed as parameter to this function. Syntax: moment(). diff(Moment|String|Number|Date|Array, String, Boolean);

What is Moment () Unix ()?

The moment(). unix() function is used to get the number of seconds since the Unix Epoch. Basically the Unix time is a system for describing a point in time.


1 Answers

You can get the diff by using the moment constructor properly.

First, when you initialize your time stamp, you should use the following format:

var timestamp = moment('15:44:06','HH:mm:ss');

Second, when you want to use the diff function with the current time you should user it like so:

timestamp.diff(moment());

Then, the given number can be converted to minutes with .asMinutes() (the first )

for further information you should read the official docs here.

like image 79
Sahar Zehavi Avatar answered Oct 14 '22 14:10

Sahar Zehavi