js代码如何实现时间段判断?

JavaScript026

js代码如何实现时间段判断?,第1张

<!DOCTYPE HTML>

<html lang="en-US">

<head>

<meta charset="UTF-8">

<title>Ball</title>

<style type="text/css">

</style>

<script type="text/javascript" src="jquery-1.8.0.min.js"></script>

<script type="text/javascript">

function timeElapse (date)

{

var reg = /^[1-9]$/

var current = new Date ()

var d = new Date (date.replace (/[^\s\d]/g, '/'))

var disp = Math.abs (current.getTime () - d.getTime ())

var days = Math.floor (disp / 1000 / 60 / 60 / 24)

var hours = Math.floor (disp / 1000 / 60 / 60 % 24)

hours = reg.test(hours) ? "0" + hours : hours

var minutes = Math.floor (disp / 1000 / 60 % 60)

minutes = reg.test(minutes) ? "0" + minutes : minutes

var seconds = Math.floor (disp / 1000 % 60)

seconds = reg.test(seconds) ? "0" + seconds : seconds

var result = "与当前时间相差 <span class=\"digit\">" + days + "</span>天 <span class=\"digit\">" + hours

+ "</span>小时 <span class=\"digit\">" + minutes + "</span>分钟 <span class=\"digit\">" + seconds

+ "</span>秒"

$ ("#clock").html (result)

TIMEOUT = setTimeout (function ()

{

timeElapse (date)

}, 1000)

}

var TIMEOUT = null

$ (function ()

{

var txt = $ (":text")

$ (":button").click (function ()

{

clearTimeout (TIMEOUT)

timeElapse (txt.val ())

})

})

</script>

</head>

<body>

<input type="text" />

<button>test</button>

<div id="clock"></div>

</body>

</html>

把分钟也获取一下就是了

比较笨的方法:

<script>

var data = new Date(),

hh = data.getHours()

mm = data.getMinutes()

mm = mm <= 9 ? "0"+mm : mm

bb = Math.floor(hh+""+mm)

console.log(bb)

if (bb >= 530 && bb <= 730) {

alert('早上好')

</script>