Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

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

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 above times by using moment.js.

If it's not possible in moment.js then please let me know using by javascript.

Inputs:

var startTime="01:30:00 am";
var endTime="2:45:07 pm";

Expected Output:

1 hour and 15 minutes
question from:https://stackoverflow.com/questions/29745873/hour-difference-between-two-timeshhmmss-ain-momentjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try below code

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

// calculate total duration
var duration = moment.duration(endTime.diff(startTime));

// duration in hours
var hours = parseInt(duration.asHours());

// duration in minutes
var minutes = parseInt(duration.asMinutes())%60;

alert (hours + ' hour and '+ minutes+' minutes.');

check fiddle here http://jsfiddle.net/nil4you/gs69Lv5x/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...