当前位置:网站首页>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年前端学习路线图:课程、源码、笔记,技术栈 另外此线路图实时更新!需要课后资料的友友们,可以直接告诉我。

边栏推荐
- Insert sort and Hill sort
- 基於 QEMUv8 搭建 OP-TEE 開發環境
- [Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
- Mongodb (III) - CRUD
- HDR image reconstruction from a single exposure using deep CNN reading notes
- HDU 2008 digital statistics
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- VIP case introduction and in-depth analysis of brokerage XX system node exceptions
- Shortcut keys in the terminal
- Installation and use of labelimg
猜你喜欢

0 basic learning C language - digital tube

About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness

GPS from entry to abandonment (XIV), ionospheric delay

UNI-Admin基础框架怎么关闭创建超级管理员入口?

Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others

ZABBIX proxy server and ZABBIX SNMP monitoring

GPS从入门到放弃(十三)、接收机自主完好性监测(RAIM)

Wechat red envelope cover applet source code - background independent version - source code with evaluation points function

2021 geometry deep learning master Michael Bronstein long article analysis

每日一题:力扣:225:用队列实现栈
随机推荐
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
2022年6月国产数据库大事记-墨天轮
Save and retrieve strings
2500 common Chinese characters + 130 common Chinese and English characters
GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
Report on technological progress and development prospects of solid oxide fuel cells in China (2022 Edition)
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Oracle control file and log file management
二分图判定
[sciter]: encapsulate the notification bar component based on sciter
What are the interface tests? What are the general test points?
The nearest common ancestor of binary (search) tree ●●
Unity3d Learning Notes 6 - GPU instantiation (1)
CCNA-思科网络 EIGRP协议
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
将MySQL的表数据纯净方式导出
[leetcode daily clock in] 1020 Number of enclaves
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Crawler obtains real estate data
Management background --4, delete classification
