Hello everyone, I could really use some assistance with Moment.js. I have two input fields, one labeled Start and the other labeled Stop.
start = moment().format('LT'); // This works when I click on the play button
stop = moment().format('LT'); // It works when I focus on the counter input next to the play button --> for testing purposes
I am looking to manually change the start input field, so I need a validation function that checks if the input value is valid or not in the format LT
. For example, if I delete the value '6:39 PM' from the input field as shown in the picture below and type '6:02:00 PM', '1:00 PM', or add a string like '5:dfds2 PM', I want to console log any error messages and revert back to the previous input value. Similarly, if I remove the current value and add a number like '1' without 'AM' or 'PM', it should determine whether the number comes before or after the stop input value and autofill the input field with '1:00 AM' or '1:00 PM'. I tried using the following function to validate the start input field but it's giving me incorrect results:
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");
}
}
Here is the image: https://i.stack.imgur.com/3eOOQ.png For more information, you can visit the Toggl website, where my idea was inspired from. Any help would be greatly appreciated! Thanks in advance.