当前位置:网站首页>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>
边栏推荐
猜你喜欢
一种跳板机的实现思路
Remote connection of raspberry pie in VNC viewer mode without display
Interface automation framework scaffolding - Implementation of parametric tools
Installing MySQL database (CentOS) in Linux source code
Information hidden in the trend chart of Hong Kong London gold market
[Unity]EBUSY: resource busy or locked
[monkey] Introduction to monkey test
【实操】Appium Settings app is not running after 5000ms
第六天 脚本与动画系统
Markdown -- basic usage syntax
随机推荐
Redis数据库
Discard Tkinter! Simple configuration to quickly generate cool GUI!
How to use output in katalon
[leetcode daily question] [December 19, 2021] 997 Find the town judge
Fabric. How to use js brush?
Dear leaders, ask me if MySQL does not support early_ Offset mode? Unsupported star
Remote connection of raspberry pie in VNC viewer mode without display
Datetime and logging module
appliedzkp zkevm(10)中的Transactions Proof
How to distinguish and define DQL, DML, DDL and DCL in SQL
Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
Sqlcmd database connection error
Katalon当中的debug调试
压缩解压
flink1.15,支持mysql视图吗?我这边在table-name处配置视图名保存,找不到表。想
DlhSoft Kanban Library for WPF
To enhance the function of jupyter notebook, here are four tips
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
工控安全之勒索病毒篇
Please consult me. I run the MYSQL to MySQL full synchronization of flykcdc in my local ide. This is in my local ide