从零开始学前端:程序猿小白也可以完全掌握!—今天你学习了吗?(JS)
复习:从零开始学前端:定时器,回调函数,math对象 — 今天你学习了吗?(JS:Day16)
文章目录
前言
第十四节课:日期对象
一、日期对象定义
获取到的本地(计算机)时间,不是北京时间,是GMT 世界时间 格林尼治:
// 一见到new,就说明该对象是object类型
// 获取到的本地(计算机)时间,不是北京时间,是GMT 世界时间 格林尼治
var date = new Date();
console.log(typeof date)
console.log(date)
效果:
年月日,时分秒:
// 日期返回年月日时分秒
var date = new Date();
// 返回年
var YY = date.getFullYear();
console.log(YY);
// 返回月份
var MM = date.getMonth()
console.log(MM);
// 返回星期
var Day = date.getDay()
console.log(Day) //礼拜天返回0
// 返回天
var DD = date.getDate();
console.log(DD)
// 返回时
var hh = date.getHours()
console.log(hh)
// 返回分
var mm = date.getMinutes()
console.log(mm)
// 返回秒
var ss = date.getSeconds()
console.log(ss)
// 返回毫秒
var ms = date.getMilliseconds()
console.log(ms)
// 返回距离1970.1.1 00:00:00 过了多少毫秒
var Time = date.getTime();
console.log(Time);
效果:
二、本地时间和指定时间
- 获取当前时间
var date = new Date();
setTimeout(function () {
// 两个对象可以进行相减!
// 定时器存在误差
console.log(new Date() - date);
}, 1000)
效果:
- 返回距离1970.1.1 00:00:00 过了多少毫秒
// 返回距离1970.1.1 00:00:00 过了多少毫秒
console.log(Date.now())
效果:
- 参照本地时间输出字符串样式
// 参照本地时间输出字符串样式
var date = new Date();
// 一次性提出年月日,时分秒
console.log(date.toLocaleString());
// 输出年月日
console.log(date.toLocaleDateString());
var b = date.toLocaleTimeString();
// 输出时分秒
console.log(b);
var c = date.toTimeString();
// 输出秒 时区
console.log(c)
效果图:
- 获取到指定的时间(访问)
// 获取到指定的时间(访问):
// 返回指定时间到1970年1月1日 00:00:00之间的毫秒值
console.log(Date.UTC(2018, 8, 19));
效果图:
- 设置时间
// 设置时间
var date = new Date(2018, 8, 19);
// 输入月份为8月,但实际为9月,座椅要在原先的基础上去减。
// 没设定时分秒的时候,默认为 00:00:00
// 返回年月 --->返回年月,其他都是默认值
console.log(date)
//一个参数,返回毫秒值
var date2 = new Date(2018);
// 返回毫秒值--->当为一个值的时候,会返回毫秒值;会返回一个时间;-->返回
console.log(2018)
var date3 = new Date('2018/8/19');
// 月份不用减1--->使用字符串的时候,月份不用减去1,而且要用/隔开
console.log(date3);
效果图:
三、getTimezoneOffset
getTimezoneOffest 返回跟世界时间的差值(单位:分钟)
// getTimezoneOffest 返回跟世界时间的差值(单位:分钟)
// 方法可返回格林威治时间和本地时间之间的时差,一分钟为单位;
var date = new Date();
console.log(date.getTimezoneOffset());
// 世界时(在本地的时候加上八小时) - 本地时间(当前时间);
// 东八区: -8个钟
// 用法,根据电脑时间算出世界各地的时间;
效果:
四、当前时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0; padding:0
}
#time{
width:500px;
height:100px;
border:1px solid #000;
margin:100px auto 0 ;
}
p{
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="time">
现在的时间为:
<!-- <p>2022年4月21日星期四:21时01分17秒</p> -->
<p></p>
</div>
<script>
var time = document.getElementById('time')
var p = document.getElementsByTagName("p")[0]
var arr = ["日","一","二","三","四","五","六"]
nowTime()
setInterval( nowTime ,1000 )
function nowTime(){
var date = new Date();
var YY = date.getFullYear()
var MM = date.getMonth() + 1
var DD = date.getDate()
var WW = date.getDay()
var hh = addZero(date.getHours())
var mm = addZero(date.getMinutes())
var ss = addZero(date.getSeconds())
// <p>2022年4月21日星期四:21时01分17秒</p>
var str = YY+"年"+MM+"月"+DD+"日 星期"+arr[WW]+","+hh+":"+mm+":"+ss
console.log( str )
p.innerHTML = str;
}
function addZero( n ){
return n < 10 ? "0"+ n : n +""
}
</script>
</body>
</html>
效果:
五、倒计时
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding:0
}
#wrap{
width:400px;
height:50px;
margin:100px auto 0;
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div id="wrap">
距离过年还有
<!-- <span> 275天02小时34分55秒 </span> -->
<span></span>
</div>
<script>
var wrap = document.getElementById("wrap")
var spans = document.getElementsByTagName("span")[0]
var newYear = new Date("2023/1/22");
fn()
function fn(){
//处理现在到明年过年还有多少天[时间差];
//时间差:现在到明年过年还有多少毫秒
var date = newYear - new Date().getTime();
//毫秒( 1000 ) =>秒 => 分钟 => 小时 => 天数
// 1 天 => 24 小时 => 60分钟 => 60秒 => 1000 毫秒;
var DD = Math.floor(date/1000/60/60/24);
var hh = Math.floor(date/1000/60/60%24)
var mm = Math.floor(date/1000/60%60)
var ss = Math.floor(date/1000%60)
// 275天02小时34分55秒
// console.log( ss )
spans.innerHTML = DD+"天"+hh+"小时"+addZero(mm)+"分"+addZero( ss )+"秒"
}
setInterval( fn , 1000 )
function addZero( n ){
return n < 10 ? "0"+ n : n +""
}
</script>
</body>
</html>