javascript - 如何向时间字符串添加分钟?

标签 javascript momentjs

我正在使用 momentjs 来操纵时间。我有一个小时、分钟、第二个字符串,如“00:30:00”。我想添加几分钟。使用 momentjs,我可以将此字符串转换为分钟,然后我可以轻松添加我想要的分钟。但我怎样才能回到刺痛的状态呢?

前 - moment.duration('00:30:00').asMinutes;会给我几分钟。 假设这给了 30 ,我可以再添加 30 分钟,使其成为 60 分钟。 如何使用 momentjs 将 60mins 再次转换为该字符串?

打印时

console.log(moment.duration('00:30:00').add(30, 'minutes'));

我正在获取

[object Object] { _bubble: function Zc(){var    a,b,c,d,e,f=this._milliseconds,g=this._days,h=this._months,i=this._data;
// if we have a mix of positive and negative values, bubble down first
// check: https://github.com/moment/moment/issues/2166
// The following code bubbles up values, see the tests for
// examples of what that means.
// convert days to months
// 12 months -> 1 year
return f>=0&&g>=0&&h>=0||0>=f&&0>=g&&0>=h||(f+=864e5*Yc(_c(h)+g),g=0,h=0),i.milliseconds=f%1e3,a=s(f/1e3),i.seconds=a%60,b=s(a/60),i.minutes=b%60,c=s(b/60),i.hours=c%24,g+=s(c/24),e=s($c(g)),h+=e,g-=Yc(_c(e)),d=s(h/12),h%=12,i.days=g,i.months=h,i.years=d,this},
  .... continued

最佳答案

您需要使用.add .

moment.duration('00:30:00').add(30, 'minutes);

然后您可以运行 .asMinutes 来打印总持续时间。

moment.duration('00:30:00').add(30, 'minutes').asMinutes();

只有当您想要获取总分钟数时,您才希望始终最后使用 .asMinutes

关于javascript - 如何向时间字符串添加分钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174943/

相关文章:

javascript - React——点击按钮定时器开始计时

javascript - 是否可以在 Angular 日期范围选择器中设置动态范围?

javascript - Moment.js unix 时间戳与纪元时间戳不匹配

javascript - 为什么 moment.js subtract 方法不起作用?

javascript - 将月份添加到日期时刻 js

javascript - 带有空白输入的 JQuery 条件

javascript - 如何用我的代码在变量中创建一个函数

javascript - 如何在 JavaScript 中重绘图表

typescript - 如何导入力矩类型定义

javascript - moment 在 Internet Explorer 和 Safari 上返回无效日期