当前位置:网站首页>Web APIs DOM 时间对象
Web APIs DOM 时间对象
2022-07-06 14:38:00 【黑马程序员官方】
前期更新笔记内容: Web API 基本认知 / 获取DOM元素 / 设置/修改DOM元素内容和元素属性 / 定时器-间歇函数 / 事件基础 / 高阶函数 / 环境对象 / 综合案例-Tab栏切换 / DOM节点
一、Web APIs 时间对象:
时间对象:用来表示时间的对象
作用:可以得到当前系统时间
1.1 实例化

- 在代码中发现了 new 关键字时,一般将这个操作称为实例化
- 创建一个时间对象并获取时间
* 获得指定时间

时间对象代码:
<!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>
</head>
<body>
<script>
// let arr = []
// let arr = new Array()
// let obj = {}
// let obj = new Object()
// new 实例化 时间对象
// 小括号为空可以得到当前的时间
let date = new Date()
console.log(date)
// 小括号里面写上时间,可以返回指定的时间
let last = new Date('2021-8-29 18:30:00')
console.log(last)
</script>
</body>
</html>
1.2 时间对象方法
因为时间对象返回的数据我们不能直接使用,所以需要转换为实际开发中常用的格式

时间对象常用方法代码:
<!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>
</head>
<body>
<script>
// new 实例化 时间对象
// 小括号为空可以得到当前的时间
let date = new Date()
console.log(date.getFullYear())
console.log(date.getMonth() + 1)
console.log(date.getDate())
// 时分秒
console.log(date.getHours())
console.log(date.getMinutes())
console.log(date.getSeconds())
// 星期几
console.log(date.getDay())
</script>
</body>
</html>
【案例】 页面显示时间
- 调用时间对象方法进行转换
- 字符串拼接后,通过 innerText 给 标签
代码:
<!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>
div {
width: 400px;
height: 50px;
background-color: pink;
text-align: center;
line-height: 50px;
}
</style>
</head>
<body>
<div></div>
<script>
let arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
let div = document.querySelector('div')
// 先调用,就省去了1秒的空白期
getTime()
setInterval(getTime, 1000)
function getTime() {
// 1. 实例化时间对象 一定写到定时器里面才可以额
let date = new Date()
let year = date.getFullYear()
let month = date.getMonth() + 1
let date1 = date.getDate()
let hour = date.getHours()
let min = date.getMinutes()
let sec = date.getSeconds()
let day = date.getDay()
div.innerHTML = `今天是: ${year}年${month}月${date1}日 ${hour}:${min}:${sec} ${arr[day]}`
}
</script>
</body>
</html>1.3 时间戳
- 是指1970年01月01日00时00分00秒起至现在的毫秒数,它是一种特殊的计量时间的方式
三种方式获取时间戳
1. 使用 getTime() 方法

2. 简写 +new Date()


- 无需实例化
- 但是只能得到当前的时间戳, 而前面两种可以返回指定时间的时间戳
时间戳代码:
<!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>
</head>
<body>
<script>
// 时间戳是总的毫秒数 是独一无二的
// 计算倒计时: 核心思想:
// 将来时间 9.1 12:00 有一个时间戳 2000000
// 现在的时间 8.29 15:00 有一个时间戳 1000000
// 可以利用将来的时间戳 减去 现在的时间戳 就是剩余的时间毫秒数
// 转换为时分秒就是剩余的时间了
// 1. getTime()
// let date = new Date()
// console.log(date.getTime())
// 2. +new Date()
console.log(+new Date()) // 当前的时间戳
console.log(+new Date('2021-8-30 12:00:00')) // 指定时间的时间戳
// 3. 只能得到当前的
// console.log(Date.now())
</script>
</body>
</html>案例 :毕业倒计时效果
注意:
1. 通过时间戳得到是毫秒,需要转换为秒在计算
2. 转换公式:
- d = parseInt(总秒数/ 60/60 /24); // 计算天数
- h = parseInt(总秒数/ 60/60 %24) // 计算小时
- m = parseInt(总秒数 /60 %60 ); // 计算分数
- s = parseInt(总秒数%60); // 计算当前秒数

案例代码:
<!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>
.countdown {
width: 240px;
height: 305px;
text-align: center;
line-height: 1;
color: #fff;
background-color: brown;
/* background-size: 240px; */
/* float: left; */
overflow: hidden;
}
.countdown .next {
font-size: 16px;
margin: 25px 0 14px;
}
.countdown .title {
font-size: 33px;
}
.countdown .tips {
margin-top: 80px;
font-size: 23px;
}
.countdown small {
font-size: 17px;
}
.countdown .clock {
width: 142px;
margin: 18px auto 0;
overflow: hidden;
}
.countdown .clock span,
.countdown .clock i {
display: block;
text-align: center;
line-height: 34px;
font-size: 23px;
float: left;
}
.countdown .clock span {
width: 34px;
height: 34px;
border-radius: 2px;
background-color: #303430;
}
.countdown .clock i {
width: 20px;
font-style: normal;
}
</style>
</head>
<body>
<div class="countdown">
<p class="next">今天是2021年8月28日</p>
<p class="title">下班倒计时</p>
<p class="clock">
<span id="hour">00</span>
<i>:</i>
<span id="minutes">25</span>
<i>:</i>
<span id="scond">20</span>
</p>
<p class="tips">
现在是18:30:00
</p>
</div>
<script>
let hour = document.querySelector('#hour')
let minutes = document.querySelector('#minutes')
let scond = document.querySelector('#scond')
time()
setInterval(time, 1000)
function time() {
// 1. 得到现在的时间戳
let now = +new Date()
// 2. 得到指定时间的时间戳
let last = +new Date('2021-8-29 18:30:00')
// 3. (计算剩余的毫秒数) / 1000 === 剩余的秒数
let count = (last - now) / 1000
// console.log(count)
// 4. 转换为时分秒
// h = parseInt(总秒数 / 60 / 60 % 24) // 计算小时
let h = parseInt(count / 60 / 60 % 24)
h = h < 10 ? '0' + h : h
// m = parseInt(总秒数 / 60 % 60); // 计算分数
let m = parseInt(count / 60 % 60)
m = m < 10 ? '0' + m : m
// s = parseInt(总秒数 % 60); // 计算当前秒数
let s = parseInt(count % 60);
s = s < 10 ? '0' + s : s
// console.log(h, m, s)
hour.innerHTML = h
minutes.innerHTML = m
scond.innerHTML = s
}
</script>
</body>
</html>黑马前端专栏干货多多,关注再学,好方便~
2022年前端学习路线图:课程、源码、笔记,技术栈 另外此线路图实时更新!需要课后资料的友友们,可以直接告诉我。

边栏推荐
- Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
- 数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
- 2022年6月国产数据库大事记-墨天轮
- BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
- A Mexican airliner bound for the United States was struck by lightning after taking off and then returned safely
- Management background --3, modify classification
- Management background --1 Create classification
- Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
- GPS from getting started to giving up (12), Doppler constant speed
- GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)
猜你喜欢

Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing

GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)

基於 QEMUv8 搭建 OP-TEE 開發環境

Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode

二分图判定

2021 geometry deep learning master Michael Bronstein long article analysis

3DMax指定面贴图

墨西哥一架飞往美国的客机起飞后遭雷击 随后安全返航

Spatial domain and frequency domain image compression of images

重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
随机推荐
GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
Chapter 3: detailed explanation of class loading process (class life cycle)
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
OpenCV VideoCapture. Get() parameter details
Management background --4, delete classification
GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
HDU 2008 digital statistics
AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
[sciter bug] multi line hiding
Oracle control file and log file management
BarcodeX(ActiveX打印控件) v5.3.0.80 免费版使用
Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
Oracle Performance Analysis 3: introduction to tkprof
Unity3D学习笔记6——GPU实例化(1)
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
414. The third largest digital buckle
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
十二、启动流程
Data storage (1)
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
