解决js时间选择器跟当前时间对比的问题
前言
当你从时间选择器上传回时间数据,想要跟当前的时间进行对比,然后比较函数可以参考下面这个
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
</style>
</head>
<body>
输入时间<input type="text" name="" id="time_str" value="2018/10/04 7:00:00">
<br>
<button onclick="resetTime()">时间对比</button>
</body>
<script type="text/javascript">
function resetTime(){
var time_str = document.getElementById("time_str").value;//别的地方传过来的时间
var e = new Date(time_str);
var n = new Date();//当前时间
if (e.valueOf() > n.valueOf()){
console.log("time_str比较大");
}else{
console.log("当前时间比较大");
}
}
</script>
</html>
效果