当前位置:网站首页>Date对象(js内置对象)
Date对象(js内置对象)
2022-04-23 06:24:00 【资深咸鱼~】
基本用法:
<script>
//当前时间对象(自动获取服务器当前时间)
var date = new Date()
console.log(date)
//UTC 1970.1.1 00:00:00(Date对象中,保存是从1970年1月1日00:00:00到现在的毫秒数)
var date1 = new Date(0)
console.log(date1)
//UTC 1970.1.1 00:00:01((1秒=1000毫秒))
var date3 = new Date(1000)
console.log(date3)
//自定义时间
var date2 = new Date("2022/4/1 12:00:00")
console.log(date2)
//返回毫秒数(1秒=1000毫秒)
//得到1970年01月1日0点零分以来的毫秒数
console.log(date2.getTime())
//计算5分钟之后的时间
var date3 = new Date()
//转换格式(Sat Apr 02 2022 11:26:21 GMT+0800 (中国标准时间))
console.log("5分钟以后的时间是:" + new Date(date3.getTime() + 5 * 60 * 1000))
//两个时间相减(得到时间戳)返回毫秒数
console.log(date3 - date2)
//获取年份
console.log(date.getFullYear() + "年")
//获取月份(月份从0开始)
console.log(date.getMonth() + 1 + "月")
//获取日
console.log(date.getDate() + "日")
//获取星期
console.log("星期" + date.getDay())
//获取小时
console.log(date.getHours() + "时")
//获取分钟
console.log(date.getMinutes() + "分")
//获取秒
console.log(date.getSeconds() + "秒")
//获取本地日期
console.log(date.toLocaleDateString())
//获取本地时间
console.log(date.toLocaleTimeString())
//获取本地日期和时间
console.log(date.toLocaleString())
</script>
打印结果
值得注意的是月份是从0开始算的,真实月份不要忘记+1!!
版权声明
本文为[资深咸鱼~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_51644702/article/details/123920561
边栏推荐
- Us photo cloud editing helps BiliBili upgrade its experience
- Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
- colab
- 数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理
- 莫比乌斯反演
- 反思|开启B站少女心模式,探究APP换肤机制的设计与实现
- P1390 公约数的和(莫比乌斯反演)
- 直观理解熵
- el-select 中v-model绑定值,数据回显只显示value,不显示label
- 配置npm
猜你喜欢

可视化常见问题解决方案(九)背景颜色问题

Emergency air space integrated communication system scheme of Guangxi Power Grid

Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference

记录一些npm 有关的问题(杂乱记录)

The people of Beifeng have been taking action

Mysql 数据库从设计上的优化

H5 case development

可视化常见问题解决方案(七)画图刻度设置解决方案

可视化常见绘图(四)柱状图

基于可视化结构的身份证号码校验系统-树莓派实现
随机推荐
嵌入式相关面经(一)
What is a closure?
vim+ctags+cscpope开发环境搭建指南
14.事务处理
Object.create()原理,Object.create()规范,手写Object.create(),Object.create()用法
Us photo cloud editing helps BiliBili upgrade its experience
Metro wireless intercom system
配置npm
[牛客练习赛68]牛牛的粉丝(矩阵快速幂之循环矩阵优化)
Typora语法详解(一)
van-uploader上传图片实现过程、使用原生input实现上传图片
菜菜的刷题日记 | 238.除自身以外数组的乘积
获取字符格式的当前时间
反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
[ACM-ICPC 2018 沈阳赛区网络预赛] J.Ka Chang (分块+dfs序)
Mysql隔离级别
学习笔记5-梯度爆炸和梯度消失(K折交叉验证)
Moment.js中format方法函数的格式
如何SQL 语句UNION实现当一个表中的一列内容为空时则取另一个表的另一列
浅谈BFC(块格式化上下文)