264

This is a common problem but I'm not sure how to solve it. The code below works fine.

var mind = time % (60 * 60);
var minutes = Math.floor(mind / 60);
         
var secd = mind % 60;
var seconds = Math.ceil(secd);

However, when I get to 1 hour or 3600 seconds it returns 0 minutes and 0 seconds. How can I avoid this so it returns all the minutes?

1
  • 1
    Thats because when time = 3600, 3600%3600 is always 0...so everything else will be 0 according to your calculation.
    – MSI
    Sep 17, 2010 at 6:59

33 Answers 33

451

To get the number of full minutes, divide the number of total seconds by 60 (60 seconds/minute):

const minutes = Math.floor(time / 60);

And to get the remaining seconds, multiply the full minutes with 60 and subtract from the total seconds:

const seconds = time - minutes * 60;

Now if you also want to get the full hours too, divide the number of total seconds by 3600 (60 minutes/hour · 60 seconds/minute) first, then calculate the remaining seconds:

const hours = Math.floor(time / 3600);
time = time - hours * 3600;

Then you calculate the full minutes and remaining seconds.

Bonus:

Use the following code to pretty-print the time (suggested by Dru):

function str_pad_left(string, pad, length) {
  return (new Array(length + 1).join(pad) + string).slice(-length);
}

const finalTime = str_pad_left(minutes, '0', 2) + ':' + str_pad_left(seconds, '0', 2);
13
  • 75
    It's a bit cleaner to get remaining seconds by doing 'var seconds = time % 60'. Dec 8, 2011 at 15:20
  • 14
    @Radio add leading zeros using function str_pad_left(string,pad,length){ return (new Array(length+1).join(pad)+string).slice(-length); } var finalTime = str_pad_left(minutes,'0',2)+':'+str_pad_left(seconds,'0',2);
    – Dru
    Dec 6, 2012 at 7:20
  • 9
    What's the sense of having negative time? Logically a time difference is always positive
    – mcont
    Dec 29, 2014 at 19:08
  • 4
    You can use modulus to get the number of seconds, it's more readable in my opinion. var seconds = time % 60 Jun 10, 2016 at 19:27
  • 6
    You can use the built-in JavaScript string.padStart() method to pad a single number with a zero.
    – user1834033
    Mar 3, 2020 at 7:19
160

Another fancy solution:

function fancyTimeFormat(duration) {
  // Hours, minutes and seconds
  const hrs = ~~(duration / 3600);
  const mins = ~~((duration % 3600) / 60);
  const secs = ~~duration % 60;

  // Output like "1:01" or "4:03:59" or "123:03:59"
  let ret = "";

  if (hrs > 0) {
    ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
  }

  ret += "" + mins + ":" + (secs < 10 ? "0" : "");
  ret += "" + secs;

  return ret;
}

console.log(
  fancyTimeFormat(1),
  fancyTimeFormat(10),
  fancyTimeFormat(100),
  fancyTimeFormat(1000),
  fancyTimeFormat(10000),
);

~~ is a shorthand for Math.floor, see this link for more info

5
  • 30
    What's the meaning of ~~?
    – mcont
    Dec 29, 2014 at 19:03
  • 15
    It's a basic shorhand for Math.floor, see this link.
    – lapin
    May 20, 2015 at 11:10
  • Its work fine..... :) You can round the value like this way hrs = hrs.toFixed(0); mins = mins.toFixed(0); secs = secs.toFixed(0); Jun 25, 2015 at 7:50
  • 2
    Thanks for this solution! I added time = math.round(time) to the first line to give me rounded seconds.
    – fotoflo
    Feb 6, 2018 at 0:53
  • 1
    This is the more correct solution. As stated above, the solution with the most votes on this page displays 180 seconds as 2m60s.
    – SongBox
    Oct 19, 2019 at 10:35
112

For people dropping in hoping for a quick simple and thus short solution to format seconds into M:SS :

function fmtMSS(s){return(s-(s%=60))/60+(9<s?':':':0')+s}

done..
The function accepts either a Number (preferred) or a String (2 conversion 'penalties' which you can halve by prepending + in the function call's argument for s as in: fmtMSS(+strSeconds)), representing positive integer seconds s as argument.

Examples:

fmtMSS(    0 );  //   0:00
fmtMSS(   '8');  //   0:08
fmtMSS(    9 );  //   0:09
fmtMSS(  '10');  //   0:10
fmtMSS(   59 );  //   0:59
fmtMSS( +'60');  //   1:00
fmtMSS(   69 );  //   1:09
fmtMSS( 3599 );  //  59:59
fmtMSS('3600');  //  60:00
fmtMSS('3661');  //  61:01
fmtMSS( 7425 );  // 123:45

Breakdown:

function fmtMSS(s){   // accepts seconds as Number or String. Returns m:ss
  return( s -         // take value s and subtract (will try to convert String to Number)
          ( s %= 60 ) // the new value of s, now holding the remainder of s divided by 60 
                      // (will also try to convert String to Number)
        ) / 60 + (    // and divide the resulting Number by 60 
                      // (can never result in a fractional value = no need for rounding)
                      // to which we concatenate a String (converts the Number to String)
                      // who's reference is chosen by the conditional operator:
          9 < s       // if    seconds is larger than 9
          ? ':'       // then  we don't need to prepend a zero
          : ':0'      // else  we do need to prepend a zero
        ) + s ;       // and we add Number s to the string (converting it to String as well)
}

Note: Negative range could be added by prepending (0>s?(s=-s,'-'):'')+ to the return expression (actually, (0>s?(s=-s,'-'):0)+ would work as well).

5
  • I'd suggest adding Math.floor(s) to the last line for cleaner results. Working great anyway, thanks! Apr 26, 2019 at 15:44
  • @PossessWithin: No, you should NOT "add Math.floor(s) to the last line" (of the breakdown-snippet) for multiple reasons, foremost because that would introduce a bug for values between 9 and 10 without further modifications; if you entered for example 69.25 then the output would be 1:9 instead of 1:09 ! This function (for which I explicitly specified integer seconds) is intended and lovingly crafted as high-perf crunching tool/'engine' (not ninja-golfed for size) and as such I consider it in-apropriate to burden that engine with unneeded verification/cleanup/filtering ...
    – GitaarLAB
    Jun 24, 2019 at 0:00
  • ... work (like NaN, +/-Infinity etc.) and rounding-options which should be done by the programmer, prior to calling the function, based on the specific needs and possible input-values that the application can encounter! Naturally you are free to patch it in if your domain knowledge about expected input-values dictates that your particular use-case almost always receives floating point values and perf-testing indicates that your application. In your case where you wish to patch in only flooring of positive values you MUST ....
    – GitaarLAB
    Jun 24, 2019 at 0:02
  • 1
    ... modify (9<s?':':':0') to (10>s?':0':':') (adding 1 char but 'sacrificing' 5 out of 6 true-paths to false-paths) OR (10<=s?':':':0') (adding 2 chars but maintaining 5 out of 6 true paths). Then I advise to change that final trailing +s to +(s|0) instead of Math.floor(s), as to not destroy the beauty of not needing to resolve and call Math.floor while still operating correctly over the entire 53bit +/-MAX_SAFE_INTEGER range instead of 'just' 32bit (unsigned) or 31bit (signed) range! Note that ABS(s) magnetude is guaranteed to be smaller than 60, so the signed...
    – GitaarLAB
    Jun 24, 2019 at 0:03
  • ... signed bitwise OR is safe. Also note that this particular modification without flooring the remaining seconds would make the function output fractional seconds without trailing fractional zeroes, but will often have more than 3 significant non-zero frational digits which you'd probably need to handle as well. Finally, when combined with the optional pre-fix code for negative range, this particular modification would effectively TRUNCate the seconds (not FLOORing them). PS: PFFT, whoever came up with just 512 char limit for any meaningful comment should be...
    – GitaarLAB
    Jun 24, 2019 at 0:04
58

2020 UPDATE

Using basic math and simple javascript this can be done in just a few lines of code.

EXAMPLE - Convert 7735 seconds to HH:MM:SS.


MATH:

Calculations use:

  1. Math.floor() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor

The Math.floor() function returns the largest integer less than or equal to a given number.

  1. % arithmetic operator (Remainder) - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder

The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.

Check out code below. Seconds are divided by 3600 to get number of hours and a remainder, which is used to calculate number of minutes and seconds.

HOURS => 7735 / 3600 = 2 remainder 535

MINUTES => 535 / 60 = 8 remainder 55

SECONDS => 55


LEADING ZEROS:

Many answers here use complicated methods to show number of hours, minutes and seconds in a proper way with leading zero - 45, 04 etc. This can be done using padStart(). This works for strings so the number must be converted to string using toString().

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.


CODE:

function secondsToTime(e){
    const h = Math.floor(e / 3600).toString().padStart(2,'0'),
          m = Math.floor(e % 3600 / 60).toString().padStart(2,'0'),
          s = Math.floor(e % 60).toString().padStart(2,'0');
    
    return h + ':' + m + ':' + s;
    //return `${h}:${m}:${s}`;
}

console.log(secondsToTime(7735));  // 02:08:55

/*
secondsToTime(SECONDS) // HH:MM:SS 

secondsToTime(8)       // 00:00:08 
secondsToTime(68)      // 00:01:08
secondsToTime(1768)    // 00:29:28
secondsToTime(3600)    // 01:00:00
secondsToTime(5296)    // 01:28:16
secondsToTime(7735)    // 02:08:55
secondsToTime(45296)   // 12:34:56
secondsToTime(145296)  // 40:21:36
secondsToTime(1145296) // 318:08:16
*/

3
  • 1
    This is so clean! The earlier answers play around with too much string/array manipulation for my taste.
    – RockyK
    Aug 29, 2020 at 18:08
  • 1
    Unfortunately, your code misses 1 second, try to convert the 3599.99999998, this will return 00:59:59, how can we solve this? Aug 4, 2021 at 14:07
  • This code works for integers and that was the original question. For decimals you need to make some more calculations.
    – Jakub Muda
    Aug 4, 2021 at 14:25
32

2019 best variant

Format hh:mm:ss

console.log(display(60 * 60 * 2.5 + 25)) // 2.5 hours + 25 seconds

function display (seconds) {
  const format = val => `0${Math.floor(val)}`.slice(-2)
  const hours = seconds / 3600
  const minutes = (seconds % 3600) / 60

  return [hours, minutes, seconds % 60].map(format).join(':')
}

6
  • hmmm, seconds is const. you can't reassign it. Apr 28, 2019 at 13:15
  • @ronapelbaum this argument, not constant, I do not understand what you mean Jun 1, 2019 at 12:45
  • 1
    A function param should be treated as const. When you use %= you're re-assigning this param. Just use seconds%60 Jul 2, 2019 at 17:34
  • 1
    @ronapelbaum Should a param always be treated as const? Isn’t it convenient sometimes for non-arrays/object literals that may suffer from mutations?
    – rnmp
    Jul 28, 2020 at 23:18
  • 1
    @RolandoMurillo usually, pure functions are better. see stackoverflow.com/questions/41625399/… Oct 27, 2020 at 11:59
24
function secondsToMinutes(time){
    return Math.floor(time / 60)+':'+Math.floor(time % 60);
}
3
  • 4
    This can be improved with zero padding the seconds: function secondsToMinutes(time){ return Math.floor(0 / 60)+':'+('0'+Math.floor(0 % 60)).slice(-2); }
    – Kus
    Dec 7, 2017 at 6:00
  • nice! thank you @Kus. Just you might want to replace those two 0s with time, am I correct?
    – mikey
    Feb 19, 2018 at 23:20
  • 5
    @mikey oops! Yes, function secondsToMinutes(time){ return Math.floor(time / 60) + ':' + ('0' + Math.floor(time % 60)).slice(-2) }
    – Kus
    Feb 21, 2018 at 22:38
22

You can also use native Date object:

var date = new Date(null);
date.setSeconds(timeInSeconds);

// retrieve time ignoring the browser timezone - returns hh:mm:ss
var utc = date.toUTCString();
// negative start index in substr does not work in IE 8 and earlier
var time = utc.substr(utc.indexOf(':') - 2, 8)

// retrieve each value individually - returns h:m:s
var time = date.getUTCHours() + ':' + date.getUTCMinutes() + ':' +  date.getUTCSeconds();

// does not work in IE8 and below - returns hh:mm:ss
var time = date.toISOString().substr(11, 8);

// not recommended - only if seconds number includes timezone difference
var time = date.toTimeString().substr(0, 8);

Of course this solution works only for timeInSeconds less than 24 hours ;)

4
  • 1
    Hadn't thought of letting the Date object handle the formatting. Less flexible, but if you want hh:mm:ss (or a subsection of that) this is great May 2, 2012 at 21:51
  • I tried this with 25 seconds and it returned 01:00:25 which equates to 1 hour and 25 seconds. Aug 19, 2013 at 12:32
  • Yes, probably because of your timezone. I have updated the solution to handle this case.
    – hamczu
    Aug 21, 2013 at 11:45
  • date could also be constructed as var date = new Date(timeInSeconds * 1000) Nov 25, 2017 at 9:27
20

2023 update:

To add leading zeros, I would just do:

const secondsToMinSecPadded = time => {
  const minutes = `${Math.floor(time / 60)}`.padStart(2, "0");
  const seconds = `${time - minutes * 60}`.padStart(2, "0");
  return `${minutes}:${seconds}`;
};


console.log(secondsToMinSecPadded(241));

Nice and short

1
  • Best one! I'd also suggest using Math.floor on the seconds too so milliseconds don't show on the final time string. Jun 25 at 17:12
14

Moment.js

If you are using Moment.js then you can use there built in Duration object

const duration = moment.duration(4825, 'seconds');

const h = duration.hours(); // 1
const m = duration.minutes(); // 20
const s = duration.seconds(); // 25
11

Clean one liner using ES6


const secondsToMinutes = seconds => Math.floor(seconds / 60) + ':' + ('0' + Math.floor(seconds % 60)).slice(-2);

9

The most concise method I found can be done using in just one line:

let timeString = `${timeInSeconds/60|0}:${timeInSeconds%60}`

Explanation

`${...}`
Template literals. Allows for expressions to be converted into a string from within the string itself.
Note: Incompatible with IE.

timeInSeconds/60|0
Takes the seconds and converts in into minutes (/60). This gives a rational number. From here it is truncated using the bitwise OR (|0)

timeInSeconds%60
Remainder (modulo). Gives the remainder of the variable divided by 60.


Hours

This method can be expanded to include hours like this:

let timeString = `${timeInSeconds/60/60|0}:${timeInSeconds/60%60|0}:${timeInSeconds%60}`

Repeating this process, you can even include days.

1
  • I built on your version. to add trailing zeros: ${resolution_duration_s / 60 | 0}:${resolution_duration_s % 60 < 10 ? "0" : ""}${resolution_duration_s % 60} and for additional trailing zero on minutes: ${resolution_duration_s / 60 < 10 ? "0" : ""}${resolution_duration_s / 60 | 0}:${resolution_duration_s % 60 < 10 ? "0" : ""}${resolution_duration_s % 60}
    – Skratt
    May 7, 2021 at 18:01
7

A one liner (doesnt work with hours):

 function sectostr(time) {
    return ~~(time / 60) + ":" + (time % 60 < 10 ? "0" : "") + time % 60;
 }
5

Seconds to h:mm:ss

var hours = Math.floor(time / 3600);
time -= hours * 3600;

var minutes = Math.floor(time / 60);
time -= minutes * 60;

var seconds = parseInt(time % 60, 10);

console.log(hours + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds));
3
  • Can someone explain minutes < 10 ? '0' + minutes : minutes for me, assuming only basic js knowledge?
    – Zediiiii
    Apr 16, 2019 at 14:23
  • If minutes are single digit (<10) prepend leading zero else not.
    – kayz1
    Apr 16, 2019 at 14:37
  • That helps! I didn't realize that "?" was the ternary operator in js.
    – Zediiiii
    Apr 16, 2019 at 15:23
5

After all this, yet another simple solution:

const time = new Date(null);
time.setSeconds(7530);
console.log(time.getHours(), time.getMinutes(), time.getSeconds());
1
  • 1
    Using time.setSeconds(1); returns 1 0 1 when it should be 0 0 1.
    – Ste
    May 21, 2020 at 19:30
4

Another but much more elegant solution for this is as follows:

/**
 * Convert number secs to display time
 *
 * 65 input becomes 01:05.
 *
 * @param Number inputSeconds Seconds input.
 */
export const toMMSS = inputSeconds => {
    const secs = parseInt( inputSeconds, 10 );
    let minutes = Math.floor( secs / 60 );
    let seconds = secs - minutes * 60;

    if ( 10 > minutes ) {
        minutes = '0' + minutes;
    }
    if ( 10 > seconds ) {
        seconds = '0' + seconds;
    }

    // Return display.
    return minutes + ':' + seconds;
};
4

The Following function will help you to get Days , Hours , Minutes , seconds

toDDHHMMSS(inputSeconds){
        const Days = Math.floor( inputSeconds / (60 * 60 * 24) );
        const Hour = Math.floor((inputSeconds % (60 * 60 * 24)) / (60 * 60));
        const Minutes = Math.floor(((inputSeconds % (60 * 60 * 24)) % (60 * 60)) / 60 );
        const Seconds = Math.floor(((inputSeconds % (60 * 60 * 24)) % (60 * 60)) % 60 );
        let ddhhmmss  = '';
        if (Days > 0){
            ddhhmmss += Days + ' Day ';
        }
        if (Hour > 0){
            ddhhmmss += Hour + ' Hour ';
        }

        if (Minutes > 0){
            ddhhmmss += Minutes + ' Minutes ';
        }

        if (Seconds > 0){
            ddhhmmss += Seconds + ' Seconds ';
        }
        return ddhhmmss;
    }
alert( toDDHHMMSS(2000));
2
  • I like this one. I changed if (Days > 0){ ddhhmmss += Days + ' Day '; } to if (Days > 0) {ddhhmmss += Days === 1 ? Days + ' Day ' : Days + ' Days '}; to say 1 Day, 2 Days, 3 Days etc. Similar can be done for the others.
    – Ste
    May 21, 2020 at 20:07
  • I add this: ddhhmmss += Days + ` Day${Days > 1 ? 's' : ''} `;
    – Diego
    Jul 24, 2020 at 1:15
4
  function formatSeconds(s: number) {
    let minutes = ~~(s / 60);
    let seconds = ~~(s % 60);
    return minutes + ':' + seconds;
  }
1
  • 5
    Welcome to Stack Overflow. Please read How to Answer.
    – Chris
    Feb 24, 2021 at 22:42
3

For adding zeros I really don't see the need to have a full other function where you can simply use for example

var mins=Math.floor(StrTime/60);
var secs=StrTime-mins * 60;
var hrs=Math.floor(StrTime / 3600);
RoundTime.innerHTML=(hrs>9?hrs:"0"+hrs) + ":" + (mins>9?mins:"0"+mins) + ":" + (secs>9?secs:"0"+secs);

Its why we have conditional statements in the first place.

(condition?if true:if false) so if example seconds is more than 9 than just show seconds else add a string 0 before it.

3

Try this:
Converting Second to HOURS, MIN and SEC.

function convertTime(sec) {
    var hours = Math.floor(sec/3600);
    (hours >= 1) ? sec = sec - (hours*3600) : hours = '00';
    var min = Math.floor(sec/60);
    (min >= 1) ? sec = sec - (min*60) : min = '00';
    (sec < 1) ? sec='00' : void 0;

    (min.toString().length == 1) ? min = '0'+min : void 0;    
    (sec.toString().length == 1) ? sec = '0'+sec : void 0;    
    
    return hours+':'+min+':'+sec;
}
2

var seconds = 60;
var measuredTime = new Date(null);
measuredTime.setSeconds(seconds); // specify value of SECONDS
var Time = measuredTime.toISOString().substr(11, 8);
document.getElementById("id1").value = Time;
<div class="form-group">
  <label for="course" class="col-md-4">Time</label>
  <div class="col-md-8">
    <input type="text" class="form-control" id="id1" name="field">Min
  </div>
</div>

2

1 - Get rest of division using %. Now you have the seconds that don't complete a minute 2 - Subtract the seconds obtained in step 1 from the total. Now you have the minutes

For example, let's assume you have 700 seconds:

seconds = 700%60); //40 seconds
minutes = (700 - (700%60))/60; //11
//11:40
2

If you're after a formatted mm:ss time string, you can use the Date constructor and toLocaleTimeString():

const seconds = 157;

const timeString = new Date(seconds * 1000).toLocaleTimeString([], {
  minute: "numeric",
  second: "2-digit",
})

console.log(timeString);

Note that:

  • we create a new Date object using milliseconds, seconds * 1000: the Date constructor accepts a timestamp. Effectively, we're creating a date that is a few minutes after January 1st, 1970, 00:00:00 UTC. (But day doesn't matter, since we only use time)
  • the first parameter of toLocaleTimeString receives a string or array of locales. If left undefined, the user agent's default locale will be used
  • the second parameter takes a DateTimeFormat options object. In our case, if we want mm:ss (without leading zero for minutes), we pass { minute: "numeric", second: "2-digit" }. (If you're still seeing a leading zero for minutes, refer to this question and this answer.)
1

I was thinking of a faster way to get this done and this is what i came up with

var sec = parseInt(time);
var min=0;
while(sec>59){ sec-=60; min++;}

If we want to convert "time" to minutes and seconds, for example:

// time = 75,3 sec
var sec = parseInt(time); //sec = 75
var min=0;
while(sec>59){ sec-=60; min++;} //sec = 15; min = 1
1

Put my two cents in :

function convertSecondsToMinutesAndSeconds(seconds){
            var minutes;
            var seconds;
            minutes = Math.floor(seconds/60);
            seconds = seconds%60;

            return [minutes, seconds];
        }

So this :

var minutesAndSeconds = convertSecondsToMinutesAndSeconds(101);

Will have the following output :

[1,41];

Then you can print it like so :

console.log('TIME : ' +  minutesSeconds[0] + ' minutes, ' + minutesSeconds[1] + ' seconds');

//TIME : 1 minutes, 41 seconds
1
export function TrainingTime(props) {
    const {train_time } = props;
    const hours = Math.floor(train_time/3600);
    const minutes = Math.floor((train_time-hours * 3600) / 60);
    const seconds = Math.floor((train_time%60));

    return `${hours} hrs  ${minutes} min  ${seconds} sec`;
}
1
1

Day.js

If you use day.js, try this.

const dayjs = require('dayjs')
const duration = require('dayjs/plugin/duration') 
dayjs.extend(duration)

const time = dayjs.duration(100, 'seconds')

time.seconds() // 40
time.minutes() // 1
time.format('mm:ss') // 01:40
1

I prefer thinking of Millisecond as its own unit, rather than as a subunit of something else. In that sense, it will have values of 0-999, so you're going to want to Pad three instead of two like I have seen with other answers. Here is an implementation:

function format(n) {
   let mil_s = String(n % 1000).padStart(3, '0');
   n = Math.trunc(n / 1000);
   let sec_s = String(n % 60).padStart(2, '0');
   n = Math.trunc(n / 60);
   return String(n) + ' m ' + sec_s + ' s ' + mil_s + ' ms';
}

console.log(format(241));

https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/String/padStart

1

Here's an ES6 version of the seconds to minutes and seconds conversion, with padding (00:00 format). It only accepts integer values for seconds and ~~(x) is the shorthand floor operation.

const padTime = n => ("" + n).padStart(2, 0);
const secondsToMinSec = time =>
  `${padTime(~~(time / 60))}:${padTime(time - ~~(time / 60) * 60)}`
;

for (let i = 0; i < 10; i++) {
  const seconds = ~~(Math.random() * 300);
  console.log(seconds, secondsToMinSec(seconds));
}

1

if you need to work with the result easily later this is what I use:

function seconds2hms(seconds, milliseconds) {
if(milliseconds) {
    seconds = Math.floor(seconds/1000);
}
return {h:~~(seconds / 3600),m:~~((seconds % 3600) / 60),s:~~seconds % 60}

}

(used Vishal's code)

0

This Workes for Me

 const start_date=moment().subtract(1,"days")
 const end_date=moment()

 const diff = end_date.diff(start_date, "seconds");
 var mind = diff % (60 * 60);

 const hours = Math.floor(diff / (60 * 60));
 const minutes = Math.floor(mind / 60);
 var seconds = Math.ceil(mind % 60);

 console.log("Diff===>", diff, hours, minutes, seconds);

..................Happy Codding...............

Not the answer you're looking for? Browse other questions tagged or ask your own question.