创建时间对象
var nowdate = new Date();
console.log(nowDate);
函数调用会显示当前的年,月,日,时,分,秒的格式。
//星期转大写
function zhuangdaxie(day){
switch (day){
case 0:{
return "日";
break;
}
case 1:{
return "一";
break;
}
case 2:{
return "二";
break;
}
default:
}
}
function getNowDate(){
//创建当前时间
var nowDate = new Date();
var year = nowDate.getFullYear();
var month = nowDate.getMonth() + 1; // 获取月份从0开始
var weekDay = zhuandaxie(nowDate.getDay());
var day = nowDate.getDate();
var hours = nowDate.getHours();
var minutes = nowDate.getMinutes();
var seconds = nowDate.getSeconds();
return year + "年" + month + "月" + day + "日 星期" +weekDay +" " + hours +":" + minutes + ":" +seconds;
var res = getNowDate();
console.log(res);
}
使用js写出时间距离2017年倒计时
<body>
<div class="time">
<span id="days">00天</span>
<span id="hours">00时</span>
<span id="minutes">00分</span>
<span id="seconds">00秒</span>
</div>
</body>
<script>
function djs(){
var EndTime= new Date('2017/1/1 00:00:00');
var NowTime = new Date();
var chazhi = EndTime.getTime() - NowTime.getTime();
if(chazhi >= 0){
day = Math.floor(chazhi/1000/60/60/24);
hour = Math.floor(chazhi/1000/60/60%24);
minute = Math.floor(chazhi/1000/60%60);
second = Math.floor(chazhi/1000%60);
}
document.getElementById("days").innerHTML = day + "天";
document.getElementById("hours").innerHTML = hour + "时";
document.getElementById("minutes").innerHTML = minute + "分";
document.getElementById("seconds").innerHTML = second + "秒";
}
setInterval(djs,0);
</script>