Date对象:获取客户端的日期时间
<script>
// 获取当前日期时间:
var now = new Date();
// console.log(now) //Fri Oct 19 2018 17:53:57 GMT+0800 (中国标准时间)
// 初始化自定义日期时间对象
var aaaa = new Date("2018/05/01");
// console.log(aaaa) //Tue May 01 2018 00:00:00 GMT+0800 (中国标准时间)
var date = new Date("2018/06/13 17:20:20");
// console.log(date) //Wed Jun 13 2018 17:20:20 GMT+0800 (中国标准时间)
// 返回自1970-1-1 00:00:00到date对象所经过的毫秒数
console.log(now.getTime()) //1539942837368
console.log(date.getTime()) //1528881620000
// 返回自1900年以来,到当前时间对象所经过的年数。
var a = now.getYear()
console.log(a, typeof a) //118 "number"
// 获取当前日期对象的四位年份数
var b = now.getFullYear()
console.log(b) //2018
// 返回0-11的数字,来表示1-12月。要想得到当前月份的值,需+1
var c = now.getMonth()
console.log(c) //9
// 返回当前日期对象的日
var d = now.getDate()
console.log(d) //19
// 返回当前日期对象的星期。返回0-6表示星期日-星期六。周一到周六正确,周日用0表示。
var e = now.getDay()
console.log(e) //5
// 获取小时
var f = now.getHours()
console.log(f) //17
// 获取分
var g = now.getMinutes()
console.log(g) //53
// 获取秒
var m = now.getSeconds()
console.log(m) //57
// 获取毫秒
var n = now.getMilliseconds()
console.log(n) //368
console.log(a.toString(),typeof a.toString()) //118 string
console.log(now.toString()) //Fri Oct 19 2018 17:53:57 GMT+0800 (中国标准时间)
console.log(now.toLocaleString()) //2018/10/19 下午5:53:57
console.log(now.toLocaleTimeString()) //下午5:53:57
console.log(now.toLocaleDateString()) //2018/10/19
console.log(now.valueOf()) //1539942837368
附加:上面获取的年、月、日、时、分、秒、星期。类型全部都是number。
</script>
常用操作:
<script type="text/javascript">
// 1.获取时间戳
var endDate1 = new Date();
// var endDate2 = new Date("2019/10/01");
// var endDate3 = new Date("2019-10-01");
var endDate2 = new Date("2019/10/01 00:00:00");
var endDate3 = new Date("2019-10-01 00:00:00");
var end1 = endDate1.getTime();
var end2 = endDate2.getTime();
var end3 = endDate3.getTime();
console.log(endDate1, end1); // Tue Dec 04 2018 15:29:08 GMT+0800 (中国标准时间) 1543908548494
console.log(endDate2, end2); // Tue Oct 01 2019 00:00:00 GMT+0800 (中国标准时间) 1569859200000
console.log(endDate3, end3); // Tue Oct 01 2019 00:00:00 GMT+0800 (中国标准时间) 1569859200000
// 2.两种时间格式转换
var st = "2009-10-20 14:38:40";
var et = "2009-10-20 15:38:40";
var a1 = st.replace("-", "/");
var a2 = et.replace(/-/g,"/");
console.log(a1, a2); // 2009/10-20 14:38:40 2009/10/20 15:38:40
// 3.快速求解两个时间之间的天数
var num = new Date("2018-09-22").getTime() - new Date("2018-08-22").getTime();
var days = num/1000/60/60/24;
console.log(days); // 31
// 方法2:如果想要中间具体的每一天
var date = '2015-05-12--2016-06-12'.split('--');
function getDate(start,end){
start = new Date(start).getTime();
end = new Date(end).getTime();
var date = [];
for(;start <= end;start += 86400000){
var tmp = new Date(start);
date.push(tmp.getFullYear()+'-'+(tmp.getMonth()+1)+'-'+tmp.getDate());
}
return date;
}
console.log(getDate(date[0],date[1]));
</script>
附加代码:
<script>
var date = new Date();
// 获取年份
var year = date.getFullYear().toString();
// 获取月份
var month = (date.getMonth()+1).toString();
if(month.length==1){
month = "0"+month;
}
// 获取日
var day = date.getDate().toString();
if(day.length==1){
day = "0"+day;
}
var hour = date.getHours().toString();
if(hour.length==1){
hour = "0"+hour;
}
var fen = date.getMinutes().toString();
if(fen.length==1){
fen = "0"+fen;
}
var miao = date.getSeconds().toString();
if(miao.length==1){
miao = "0"+miao;
}
t = year+"-"+month+"-"+day+" "+hour+":"+fen+":"+miao;
console.log(t) //2018-10-19 19:38:37
console.log(typeof t) //string
console.log(typeof(t)) //string
</script>
js比较时间日期的大小
<script>
// 第一种:转换为date对象进行比较操作
var st="2009-10-20 14:38:40";
var et="2009-10-20 15:38:40";
var stdt=new Date(st.replace("-","/"));
var etdt=new Date(et.replace("-","/"));
if(stdt>etdt) {
alert("开始时间必须小于结束时间");
}
// 第二种:直接比较大小即可
var st="2009-10-20 14:38:40";
var et="2009-10-20 15:38:40";
if(st>et) alert("开始时间必须小于结束时间");
</script>
js倒计时的两种实现方式
<!-- 一:设置时长,进行倒计时。比如考试时间等等 -->
<text class="time" id='time'></text>
<script>
var countdown = document.getElementById("time");
var time = 600; //10分钟换算成600秒
var tim = setInterval(function () {
time = time - 1;
var minute = parseInt(time / 60);
minute = minute > 9 ? minute : "0" + minute;
var second = parseInt(time % 60);
second = second > 9 ? second : "0" + second;
countdown.innerHTML = minute + ":" + second;
if (minute == 0 && second == 0) {
countdown.innerHTML = "已过期";
clearInterval(tim);
return;
}
}, 1000);
</script>
<div id="timer" style="color:red"></div>
<script type="text/javascript">
var maxtime = 60 * 60; //一个小时,按秒计算,自己调整!
function CountDown() {
if (maxtime >= 0) {
minutes = Math.floor(maxtime / 60);
seconds = Math.floor(maxtime % 60);
msg = "距离结束还有" + minutes + "分" + seconds + "秒";
document.all["timer"].innerHTML = msg;
if (maxtime == 5 * 60)alert("还剩5分钟");
--maxtime;
} else{
clearInterval(timer);
alert("时间到,结束!");
}
}
timer = setInterval("CountDown()", 1000);
</script>
<!-- 二:设置时间戳,进行倒计时。比如距离活动结束时间等等 -->
<html>
<head>
<meta charset="UTF-8">
<title>js简单时分秒倒计时</title>
<script type="text/javascript">
function countTime() {
//获取当前时间
var date = new Date();
var now = date.getTime();
//设置截止时间
var str="2019/12/17 00:00:00";
var endDate = new Date(str);
var end = endDate.getTime();
//时间差
var leftTime = end-now;
//定义变量 d,h,m,s保存倒计时的时间
var d,h,m,s;
if (leftTime>=0) {
d = Math.floor(leftTime/1000/60/60/24);
h = Math.floor(leftTime/1000/60/60%24);
m = Math.floor(leftTime/1000/60%60);
s = Math.floor(leftTime/1000%60);
}
//将0-9的数字前面加上0,例1变为01
d = checkTime(d);
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
function checkTime(i){
if (i<10) {
i = "0"+i;
}
return i;
}
//将倒计时赋值到div中
document.getElementById("_d").innerHTML = d+"天";
document.getElementById("_h").innerHTML = h+"时";
document.getElementById("_m").innerHTML = m+"分";
document.getElementById("_s").innerHTML = s+"秒";
//递归每秒调用countTime方法,显示动态时间效果
setTimeout(countTime,1000);
}
</script>
</head >
<body onload="countTime()" >
<div>
<span id="_d">00</span>
<span id="_h">00</span>
<span id="_m">00</span>
<span id="_s">00</span>
</div>
</body>
</html>