Hour difference between two times(HH:MM:SS a)in momentjs

后端 未结 11 2456
温柔的废话
温柔的废话 2020-12-03 03:02

I have two time without date

var startTime=\"12:16:59 am\";
var endTime=\"06:12:07 pm\";

I want to show the total hours in between the abov

11条回答
  •  情话喂你
    2020-12-03 03:19

    //Get start time and end time
    var startTime = moment("12:16:59 am", "HH:mm:ss a"),
    endTime = moment("06:12:07 pm", "HH:mm:ss a");
    
    1. Method 1

    var dif = moment.duration(endTime.diff(startTime));
    console.log([dif.hours(), dif.minutes(), dif.seconds()].join(':'));
    console.log('dif in Mins: ', (dif.hours() * 60) + dif.minutes());
    
    1. Method 2

    console.log('hrs: ', moment(endTime).diff(startTime, 'hours'));
    console.log('dif in Mins: ', moment(endTime).diff(moment(startTime), 'minutes'));
    
    1. Method 3

    var hrs = moment.utc(endTime.diff(startTime)).format("HH");
    var min = moment.utc(endTime.diff(startTime)).format("mm");
    var sec = moment.utc(endTime.diff(startTime)).format("ss");
    console.log([hrs, min, sec].join(':'));
    
    1. Formatted output

    var dif = moment.duration(endTime.diff(startTime));
    console.log(dif.humanize());
    

提交回复
热议问题