admin管理员组

文章数量:1389768

I am using React Js and I would like to perform a subtract time in JavaScript with moment library.

I tried my code as follows:

timecheck(){
    var time1 = moment().format("09:00:00");
    var time2 = moment().format("00:03:15");
    var timeStr = time2.split(':');
    var h = timeStr[0];
    var m = timeStr[1];
    var s = timeStr[2];

    var time3 = moment(time1).subtract({'hours': h, 'minutes': m, 'second': s}).format('hh:mm:ss');
    console.log(time3);
  }

The above code was my timecheck function, I would like to perform time3 = time1 - time2, it console log as InvalidDate

May I know where is my syntax error?

I am using React Js and I would like to perform a subtract time in JavaScript with moment library.

I tried my code as follows:

timecheck(){
    var time1 = moment().format("09:00:00");
    var time2 = moment().format("00:03:15");
    var timeStr = time2.split(':');
    var h = timeStr[0];
    var m = timeStr[1];
    var s = timeStr[2];

    var time3 = moment(time1).subtract({'hours': h, 'minutes': m, 'second': s}).format('hh:mm:ss');
    console.log(time3);
  }

The above code was my timecheck function, I would like to perform time3 = time1 - time2, it console log as InvalidDate

May I know where is my syntax error?

Share Improve this question edited Aug 23, 2020 at 7:06 Always Helping 14.6k4 gold badges15 silver badges30 bronze badges asked Aug 23, 2020 at 5:28 LawraokeLawraoke 5567 silver badges27 bronze badges 4
  • whats your expected output after subtraction ? – Always Helping Commented Aug 23, 2020 at 5:40
  • Hi @AlwaysHelping I would like to have 'hh:mm:ss' format – Lawraoke Commented Aug 23, 2020 at 5:44
  • I understand that! But whats your final time expected out after subtraction ? – Always Helping Commented Aug 23, 2020 at 5:45
  • @AlwaysHelping so it is 9h minus 3 minutes 15 seconds it will be 8h : 56min : 45sec something like that or '08:56:45' – Lawraoke Commented Aug 23, 2020 at 5:46
Add a ment  | 

4 Answers 4

Reset to default 4

You can simply use .subtract function to do get the results you are after. You do not need to use split or anything like that!

Also, we need define the format of our times we are subtracting from as hh:mm:ss in the moment object otherwise you will get a deprecation warning.

let time1 = moment("09:00:00", "hh:mm:ss");
let time2 = moment("00:03:15", "hh:mm:ss");
let subtract = time1.subtract(time2);
let format = moment(subtract).format("hh:mm:ss")
console.log(format); //08:56:45
<script src="//cdnjs.cloudflare./ajax/libs/moment.js/2.8.4/moment.js"></script>

    const time1 = moment("09:00:00", 'hh:mm:ss'); //create the first time
    const time2 = moment("00:03:15", 'hh:mm:ss'); //create the second time

    const hours = time1.diff(time2, 'hours'); //Get the hours diff and set the hours for second date
    time2.add(hours, 'hours');

    const minutes = time1.diff(time2, 'minutes');
    time2.add(minutes, 'minutes');

    const seconds = time1.diff(time2, 'seconds');
    time2.add(seconds, 'seconds');

    // Get the differences
    const diff = [
        hours,
        minutes,
        seconds
    ];

    // Get the diff as a string
    const time3 = moment(diff, 'hh:mm:ss').format('hh:mm:ss');
    console.log(time3);

I notice that your h, m, and s variables are strings rather than numbers. You seem to want numbers rather than strings. Try something like

var h = timeStr[0] - 0;

or whatever is your favorite way of coercing a string to a numeric value.

Moment has a difference function:

var time1 = moment("09:00:00", "hh:mm:ss");
var time2 = moment("00:03:15", "hh:mm:ss");
var diff = time2.diff(time1);

diff will be the number of milliseconds between the two date. So then it should just be a case of:

var time3 = moment(diff).format('hh:mm:ss');

See https://momentjs./docs/#/displaying/difference/ for more details.

本文标签: JavaScript subtract time in moment used in reactJSStack Overflow