Javascript 如何将当前小时与小时字符串进行比较
我有属性字符串Time:JavaScript中的“10:50”响应。 如何比较字符串的小时数和系统的小时数。 例如:如果10=10,则在函数中运行代码。Javascript 如何将当前小时与小时字符串进行比较,javascript,Javascript,我有属性字符串Time:JavaScript中的“10:50”响应。 如何比较字符串的小时数和系统的小时数。 例如:如果10=10,则在函数中运行代码。 请帮忙。谢谢您应该将字符串包装到日期对象中,并对日期对象使用getHours()函数。差不多 //in the oldDate, pass the string as params to the date constructor let oldDate = new Date("date string") //construct this on
请帮忙。谢谢您应该将字符串包装到日期对象中,并对日期对象使用getHours()函数。差不多
//in the oldDate, pass the string as params to the date constructor
let oldDate = new Date("date string")
//construct this one without a parameter and it will use the system time
let systemDate = new Date()
if(oldDate.getHours() == systemDate.getHours())
但是,如果您非常确定响应的形式是“10:50”
,那么这将是更好的选择
let time = "10:50"
let h = parseInt(time.split(":")[0])
let systemDate = new Date()
if(h == systemDate.getHours())
let dateTime=new Date()代码>
let hour=dateTime.getHours()代码>
用代码提供更多信息,以便我们能够理解您所说的。。。