当前位置:网站首页>JS基础6
JS基础6
2022-06-28 10:50:00 【程序员社区】
日期时间对象Date
创建
- 自动获取时间
var date=new Date()
- 创建指定日期时间
var date=new Date('2021,3,18 00:00:00')
注:
里面必须是字符串类型
传入的值以年,月,日,时,分,秒,没有的补零处理
方法
| 方法 | 含义 |
|---|---|
| getTime() | 返回日期毫秒数 |
| getFullYear() | 返回4位数年 |
| getMonth() | 返回日期的月 |
| getDate() | 返回日期的日 |
| getDay() | 返回日期的周几 |
| getHours() | 返回日期中的时 |
| getMinutes() | 返回日期中的分 |
| getSeconds() | 返回日期中的秒 |
| getMilliseconds() | 返回日期中的毫秒 |
注:
- 月份是从0开始计数
- getDay()中0表示周日,其他不变
计算两个日期差
- 准换日期为毫秒
注:毫秒为日期到计算机时间原点(1970年1月1日0时0分0秒) - 相差的毫秒数
- 转换
<script> function timeDiffer(m,n){
var date1=new Date(m)
var date2=new Date(n)
//计算两个日期差的毫秒
var differdate=date1.getTime()-date2.getTime()
//一年多少毫秒
var yearTime=1000*60*60*24*365
//多少年向下取整
var year = Math.floor(differdate/yearTime)
console.log(year)
//一个月多少毫秒
var monthTime=1000*60*60*24*30
//两个日期差的毫秒减去年的毫秒
var differmonth=differdate-year*yearTime
var month =Math.floor(differmonth/monthTime)
console.log(month)
var dayTime=1000*60*60*24
var differday=differmonth-month*monthTime
var day=Math.floor(differday/dayTime)
console.log(day)
var hourTime=1000*60*60
var differhour=differday-day*dayTime
var hour=Math.floor(differhour/hourTime)
console.log(hour)
var minuteTime=1000*60
var differMinute=differhour-hour*hourTime
var minute=Math.floor(differMinute/minuteTime)
console.log(minute)
var secondTime=1000
var differSecond=differMinute-minute*minuteTime
var second=Math.floor(differSecond/secondTime)
console.log(second)
}
timeDiffer('2022-4-18 11:34:02','2022-2-18 8:30:00')
</script>
日期格式化方法
| 方法 | 含义 |
|---|---|
| toString | 返回带时区信息的日期和时间 |
| toDateString | 显示日期中的周几,月,日,年 |
| toTimeString | 显示时,分,秒,和时区 |
| toLocaleDateString | 显示日期中的周几,月,日,年 |
| toLocaleTimeString | 显示时,分,秒 |
| toUTCString | 显示完整的UTC日期 |
<script>
var date=new Date()
var date1=date.toString()
var date2=date.toDateString()
var date3=date.toTimeString()
var date4=date.toLocaleDateString()
var date5=date.toLocaleTimeString()
var date6=date.toUTCString()
console.log(date)
//结果Sat Mar 19 2022 10:21:30 GMT+0800 (中国标准时间)
console.log(date1)
//结果Sat Mar 19 2022 10:21:30 GMT+0800 (中国标准时间)
console.log(date2)
//结果Sat Mar 19 2022
console.log(date3)
//结果10:21:30 GMT+0800 (中国标准时间)
console.log(date4)
//结果2022/3/19
console.log(date5)
//结果10:21:30
console.log(date6)
//结果Sat, 19 Mar 2022 02:21:30 GMT
</script>
边栏推荐
- Realization of a springboard machine
- [cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
- [Unity][ECS]学习笔记(三)
- [practice] appium settings app is not running after 5000ms
- What is the best way to learn machine learning
- Spatial-Temporal时间序列预测建模方法汇总
- JSON模块、hashlib、base64
- linux中源码安装mysql数据库(centos)
- Compression and decompression
- 【LeetCode每日一题】【2021/12/19】997. 找到小镇的法官
猜你喜欢

Ideal interface automation project

Metersphere implements UI automation elements that are not clickable (partially occluded)

I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!

树莓派无需显示屏的VNC Viewer方式的远程连接

Katalon框架测试web(二十)自定义关键字以及上传弹窗操作

How to use dataant to monitor Apache apisex

Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)

如何使用 DataAnt 监控 Apache APISIX

Mysql database overview and installation process

港伦敦金行情走势图所隐藏的信息
随机推荐
移动命令
【实战】1364- 实现一个完美的移动端瀑布流组件(附源码)
建立自己的网站(11)
[unity][ecs] learning notes (I)
Using loops for, while, and if in katalon else、break、continue
Ribbon core source code analysis
Katalon当中的使用循环for、while和if...else、break、continue
MySQL(三)
[function suggestion] select a space when multiple workspaces are started
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
Bytecode proof in appliedzkp zkevm (9)
DlhSoft Kanban Library for WPF
Ribbon核心源码解析
Sqlcmd database connection error
[cloud resident co creation] detailed introduction to DWS alarm service DMS and cluster connection mode
[leetcode daily question] [December 19, 2021] 997 Find the town judge
[Unity][ECS]学习笔记(二)
MySQL (II)
[Unity]EBUSY: resource busy or locked
June training (day 28) - Dynamic Planning