Search code examples
javascriptmomentjsangular-momentmoment-timezone

How to subtract two hours format in hours format in moment


let dayDuration = "23:59:59";
let anotherhour ="04:00:00";

moment.duration(moment(dayDuration, "hh:mm:ss").diff(anotherhour));

How to incremant in previous value in a loop:

suppose first iteration duration value as 10 and second iteration duration value 30 it should add as 40.

     hasEvnets.map(x=>{
var dayDuration = x.startTime;
var anotherHour=x.endTime;
     var remainingtime+= moment.duration(moment(dayDuration, "hh:mm:ss").diff(anotherhour));
        });

And

    moment(moment("00:00:00", "HH:mm:ss").diff(moment("18:00:00", "HH:mm:ss"))).format("HH:mm:ss");
    moment(moment("00:00:00", "HH:mm:ss").diff(moment("18:00:00", "HH:mm:ss"))).utc().format("HH:mm:ss"); -06:00:00
which produces 00:00:00 as o/p but i need -18:00:00 how to do that formatting.

In C# , which works fine.

 var dayDuration = new TimeSpan(00, 00, 00).Subtract(Convert.ToDateTime("2019-02-01 18:00:00").TimeOfDay);
            Console.WriteLine(dayDuration);

And I have tried this .

let n = moment.duration(moment("00:00:00", "HH:mm:ss").diff(moment("18:00:00", "HH:mm:ss")));

console.log(n);

enter image description here

I need below format. 18:00:00

http://jsfiddle.net/5103y2du/4/

Atlast I have found solution but the solution is not good.

let n = moment.duration(moment("00:00:00", "HH:mm:ss").diff(moment("18:00:00", "HH:mm:ss")));

working fine

let z = n.hours();
let y = n.minutes();
let u = n.seconds();
console.log(z+":0"+y+":0"+u);

-- Not working

Need the same by using moment

console.log(moment(n.hours).format("hh:mm:ss"));

Solution

  • Your anotherhour is a string, so it's not working properly.

    Try this:

    moment(moment(dayDuration, "HH:mm:ss").diff(moment(anotherhour, "HH:mm:ss"))).utc().format('HH:mm:ss')