<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>
js 判断当前时间(或者所选时间)是否在某一时间范围
传入 beginDateStr (开始时间), endDateStr(结束时间)
使用方法如下
date.isDuringDate('2018/09/17', '2030/09/17')// 当前时间是否在2018/09/17 - 2030/09/17 之间,输出 true
date.isDuringDate('2018/09/17 13:00', '2019/09/17 15:00')
// 当前时间是否在2018/09/17 13:00 - 2019/09/17 15:00 之间,输出 false
date.isDuringDate('2018-09-17 13:00', '2019-09-17 15:00')
// 当前时间是否在2018/09/17 13:00 - 2019-09-17 15:00 之间,输出 false
参考文章:
JavaScript比较当前时间是否在指定时间段内
详细源码:
/*** [isDuringDate 比较当前时间是否在指定时间段内]
* @author dongsir
* @DateTime 2019-08-21
* @version 1.0
* @param date [beginDateStr] [开始时间]
* @param date [endDateStr] [结束时间]
* @return Boolean
*/