Moment.js以小时为单位添加时间(以小时为单位):分钟

时间:2017-06-11 15:33:42

标签: javascript momentjs

考虑用户输入,

var firstTrainTime = 12:00 //input as **hours:minutes**
var frequency = 30 //input as any number (user could for instance 
enter 1345 minutes) of **minutes**

这两个变量都以字符串形式输入。

我试图使用Moment.js制作"下次到达"火车的时间,以便输出

var arrival = 12:30 //this will be in 24hr format ("HH:mm")

我的代码:

var a = moment(firstTrainTime, "HH:mm");
console.log("this is firstTrainTime " + a);

var b = moment(freq, "m");
console.log("this is freq " + b)

var nextArrival = a.from(b);
console.log("this is nextArrival " + nextArrival);

nextArrival = moment().format("HH:mm");
console.log("this is nextArrival " + nextArrival);

Var& a& b返回时刻对象 - 但是,当我运行"来自"对它们的方法它从现在起12小时后返回"在控制台。

我使用javascript / jquery为webapp编码。

非常感谢!任何指导赞赏。

1 个答案:

答案 0 :(得分:2)

var firstTrainTime = 12:00 //input as **hours:minutes**
var frequency = 30         //input as any number (user could for instance enter 1345 minutes) of **minutes**

moment(firstTrainTime, 'HH:mm').add(frequency, 'minutes').format("HH:mm")
//"12:30"

关于#add()的文档:https://momentjs.com/docs/#/manipulating/add/