大家好,我需要 moment js 的帮助,我有一个名为 Start 的输入字段,另一个名为 stop
start = moment().format('LT'); // works when i click on the play button
stop = moment().format('LT'); // works when i focus on the counter input beside the play button --> for test
我想手动更改开始输入字段,所以我想要一个验证函数,它接受输入值并检查输入是否在此表单 LT
上有效,例如:当我删除输入的值是 6:39 PM
如下图所示,然后键入例如 6:02:00 PM 或 1:00 PM 或
添加一个字符串 5:dfds2 PM
我想控制台记录任何错误消息并再次将先前的值返回到输入
当我删除当前值并添加一个数字,例如“1”时没有 am 或 pm
,因此它确定数字是在停止输入值之前还是之后,然后在输入字段中键入我使用的 1:00 AM 或 1:00PM
此函数用于验证开始输入字段,但它给了我错误的答案
function validate(inputVal) {
let temp =this.startTime;
let x = temp;
if(moment(inputVal, "hh:mm").isValid()) {
x= moment(inputVal, "HH:mm").format('hh:mm A');
console.log("inputVal is: " + inputVal + " and x is: " + x);
this.startTime = x
} else {
this.startTime = "temp";
console.log("no");
}
}
这是照片 有关更多信息,您可以访问 toggl website我的想法是从那里得到的 有什么帮助吗?!提前谢谢
最佳答案
我继续稍微清理了你的函数,减少了逻辑,现在你应该确保时刻格式是你要找的
function validate(val) {
let parsedTime = moment(val, "hh:mm");
if (parsedTime.isValid()) {
this.startTime = parsedTime.format('hh:mm A');
}
}
关于javascript - 如何减去两倍力矩js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45527629/