我有两个时间选择器,我需要检查从开始到结束选择的时间是否在正确的范围内,我检查了数值差,但返回的数值为NaN。显示的时间为8:00 PM至10:00 PM等。
@id/contact_name
答案 0 :(得分:0)
由于不是最好的方法,因为使用AM和PM处理时间需要做更多的工作,还需要检查first_time var hm_1 = '09:00 PM';
var hm_2 = '12:00 PM';
var a = hm_1.split(/:| /); // split at : and " "
var b = hm_2.split(/:| /);
// convert hours and minutes into seconds
var sec_1 = a[0] * 60 * 60 + a[1] * 60;
var sec_2 = b[0] * 60 * 60 + b[1] * 60;
console.log("convert", hm_1, "and", hm_2, "in", "seconds", sec_1, sec_2);
var date = new Date();
var date = new Date((sec_2 - sec_1) * 1000); //use js new Date () can take s/h/... as input
var hh = date.getUTCHours(); //get hours
var mm = date.getUTCMinutes(); //get minutes
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
// convert to hh:mm (its a 24h format)
var timeString = hh+":"+mm;
console.log("result", timeString)