要比较当前时间与输入的ngb-timepicker中的时间,可以使用JavaScript的Date对象进行比较。以下是一个示例代码:
export class YourComponent {
selectedTime: {hour: number, minute: number}; // 用于存储选择的时间
compareTime() {
const currentTime = new Date(); // 当前时间
const selectedDateTime = new Date(); // 初始化选定的时间,将其设置为当前日期
// 设置选定的时间的小时和分钟
selectedDateTime.setHours(this.selectedTime.hour);
selectedDateTime.setMinutes(this.selectedTime.minute);
if (selectedDateTime > currentTime) {
console.log("选定的时间晚于当前时间");
} else if (selectedDateTime < currentTime) {
console.log("选定的时间早于当前时间");
} else {
console.log("选定的时间与当前时间相同");
}
}
}
在上面的示例中,我们在ngModelChange事件触发时调用compareTime()方法。在该方法中,我们首先获取当前时间,并创建一个新的Date对象来存储选择的时间,并将其设置为当前日期。然后我们将选择的时间的小时和分钟设置为ngb-timepicker中选择的小时和分钟。最后,我们通过比较两个时间的大小来确定选定的时间是早于、晚于还是与当前时间相同,并在控制台上输出相应的消息。