在moment js中使用'HH:mm'字符串增加一小时(moment.js)

时间:2019-06-17 14:08:55

标签: javascript momentjs

我已经测试了这段代码:

a = '10:10'
console.log( moment( a ).subtract(1, 'h').format('HH:mm'));

但是它不起作用,输出为:

Invalid date

控制台错误:

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 10:10, _f: undefined, _strict: undefined, _locale: [object Object]

2 个答案:

答案 0 :(得分:2)

无法理解您为moment的构造函数提供的时间格式。请指定时间格式作为第二个参数,您的代码应运行。

我猜测您使用的时间格式为HH:mm,表示24小时制的小时,以及介于:之间的分钟。

var a = '10:10'
var b = moment( a, 'HH:mm' ).subtract(1, 'h').format('HH:mm')
console.log(b);
alert(b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

答案 1 :(得分:0)

欢迎堆栈溢出! 您的日期格式不正确。试试这个

{{1}}

Sandbox