javascript - 在时刻 js 中使用 'HH:mm' 字符串增加一小时(moment.js)
问题描述
我已经测试了这段代码:
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]
解决方案
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>
推荐阅读
- c++ - 英特尔 MKL 是否值得用于小型向量/矩阵 (2-4 D)
- r - 通过变量值向 ggplot 提供十六进制颜色代码
- python - 在不同的字符串中找到三个元字符中的任何一个
- c++ - 在决策树的实现中重用节点
- postgresql - 沙发到 postgres 同步在服务器上不起作用
- firebase - Firebase 软件包未导入?
- windows-10 - PgAdmin 不加载
- java - IntelliJ JavaFX Gradle 项目中的 NullPointerException 加载资源
- delphi - FMX Delphi 应用程序可以在 Win、Mac、iOS 和 Android 上录制和播放哪种音频文件格式?
- pine-script - 是否有基于时间选择蜡烛的内置功能?