最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add hour and minutes to a string with hh:mm? - Stack Overflow

programmeradmin1浏览0评论

I have a string containing 13:00 , i have an duration of 90 for example and i have to convert the duration to hh:mm which i made it. Now i have a string with 1:30. How can i add this 1:30 string to the other 13:00 string so i can pare it with another strings? This is how i conver the duration

var bookH = Math.floor(data[0]['Duration'] / 60);
    var bookM = data[0]['Duration'] % 60;
    var bookingDurationToHour = bookH + ':' + bookM;

There is momentjs in the project but i still do not know how can i do that.

I have a string containing 13:00 , i have an duration of 90 for example and i have to convert the duration to hh:mm which i made it. Now i have a string with 1:30. How can i add this 1:30 string to the other 13:00 string so i can pare it with another strings? This is how i conver the duration

var bookH = Math.floor(data[0]['Duration'] / 60);
    var bookM = data[0]['Duration'] % 60;
    var bookingDurationToHour = bookH + ':' + bookM;

There is momentjs in the project but i still do not know how can i do that.

Share Improve this question edited May 25, 2017 at 13:08 Expressingx asked May 25, 2017 at 13:02 ExpressingxExpressingx 1,5722 gold badges17 silver badges47 bronze badges 1
  • 1 Possibly consider using Moment.js for these kind of operations. – Karl Reid Commented May 25, 2017 at 13:07
Add a ment  | 

2 Answers 2

Reset to default 7

A solution with plain javascript.

var time = "13:00";
var totalInMinutes = (parseInt(time.split(":")[0]) * 60) + parseInt(time.split(":")[1]);

var otherMinutes = 90;

var grandTotal = otherMinutes + totalInMinutes;

//Now using your own code

var bookH = Math.floor(grandTotal / 60);
var bookM = grandTotal % 60;
var bookingDurationToHour = bookH + ':' + bookM;

alert(bookingDurationToHour);

Plunker

I've used Datejs before as vanilla JS with dates can be a bit difficult

First you want to put your time into a date time object

var myDate = Date.parse("2017-05-25 " + time);

Then you can add your time using the Datejs library:

myDate = myDate.addMinutes(90);

Then toString your new date as a time:

var newTime = myDate.toString("HH:MM");
发布评论

评论列表(0)

  1. 暂无评论