文章目录
- Date日期对象
- Date对象的创建
- 格式化日期
- 3.获取Date总的毫秒数(时间戳),是距离1970年1月1日过了多少毫秒数。
- 二:常用时间获取方法
- 三:日期设置方法
- 四:时间转字符串
菜鸟工具:https://www.runoob.com/jsref/jsref-obj-date.html
Date日期对象
-
Date()日期对象 是一个构造函数,必须使用new 来调用创建我们的日期对象
-
使用Date日期对象的时候,如果里面没有放置参数,那么放回的就是系统的当前时间
var date= new Date();
console.log(date);
参数常用的写法 数字型 2022,10,01 或者是 字符串型 '2022-10-01 16:45:20’
var date1=new Date(2022,10,01);
console.log(date1)
var date1=new Date(2022-10-01 16:45:20);
console.log(date1)
Date对象的创建
- Date 对象会自动把当前日期和时间保存为其初始值。注意当前日期和时间是指创建date对象那个时间点的时间。date对象创建后,它保存的时间并不会变化。
let date = new Date() // Date 对象会自动把当前日期和时间保存为其初始值。
console.log(date);
setTimeout(() => {
console.log(new Date())
console.log(date);
}, 3000)
Date可以通过四种方式(如语法所述)进行构造
-
无参
new Date()
:创建的 Date 对象表示实例化时刻的日期和时间。 -
Unix时间戳
new Date(value)
:一个Unix 时间戳(Unix Time Stamp),它是一个整数值,表示自 1970 年 1 月 1 日 00:00:00UTC(the Unix epoch)以来的毫秒数,忽略了闰秒。请注意大多数 Unix 时间戳功能仅精确到最接近的秒。
3.时间戳字符串new Date(dateString)
:表示日期的字符串值。该字符串应该能被 Date.parse() 正确方法识别(即符合 IETF-compliant RFC 2822 timestamps 或 version of ISO8601)。
4.日期与时分秒 newDate(year, monthIndex [, day [, hours [, minutes [, seconds [,milliseconds]]]]])
:
语法
new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
格式化日期
var date = new Date()
// 调用Date()这个日期函数
console.log(date.getFullYear())
// // //返回当前日期的年份
console.log(date.getMonth() + 1)
// // //返回当前日期的月份,但是返回值是在0-11,我们需要在获取到的月份上 进行+1的操作,然后输出结果。
console.log(date.getDate())
// // //返回的是当前日期的天数,也就是今天是几号
console.log(date.getDay())
// //返回的是今天是星期几 周一返回的是1,周六返回的是6 但是周日返回的是0
console.log(date.getHours())
// //返回的是当前的小时
console.log(date.getMinutes())
// //返回的是当前的分钟数
console.log(date.getSeconds())
//返回的是当前的秒针数
3.获取Date总的毫秒数(时间戳),是距离1970年1月1日过了多少毫秒数。
var date = new Date();
console.log(date.valueOf());
console.log(date.getTime());
// 我们现在距离1970年1月1日过了多少毫秒数
var date1 = +new Date()
// +new Date() 返回的就是总的毫秒数
console.log(date1)
二:常用时间获取方法
- Date() 返回当日的日期和时间。
- getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。
- getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。(周日为0;)
- getMonth() 从 Date 对象返回月份 (0 ~ 11)。
- getFullYear() 从 Date 对象以四位数字返回年份。
- getYear() 请使用 getFullYear() 方法代替。
- getHours() 返回 Date 对象的小时 (0 ~ 23)。
- getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。
- getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。
- getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。
- getTime() 返回 1970 年 1 月 1 日至今的毫秒数。
new Date().getFullYear()//2018 年
new Date().getMonth()//10 月 (月份 1 实际为11)
new Date().getDate()//5 日
new Date().getHours()//16 时
new Date().getMinutes()//2 分
new Date().getSeconds()//58 秒
new Date().getMilliseconds()//940 毫秒
new Date().getDay()//1 //星期
new Date().getTime()//1541405023106 // 从 '1970/01/01 00:00:00' 到该时间的毫秒
new Date().getTimezoneOffset()/60 //-8 格林威治时间与本地时间差
new Date(1543636820000).toLocaleString() //"2018/12/1 下午12:00:20" 格式化
new Date('2018/12/1 12:00:00').valueOf()//1543636800000 与getTime方法类似
new Date('2018/12/1 12:00:00').toLocaleDateString()//"2018/12/1" 日期格式化
new Date('2018/12/1 12:00:00').toLocaleTimeString()//"下午12:00:00" 时间格式
三:日期设置方法
- setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。
- setMonth() 设置 Date 对象中月份 (0 ~ 11)。
- setFullYear() 设置 Date 对象中的年份(四位数字)。
- setYear() 请使用 setFullYear() 方法代替。
- setHours() 设置 Date 对象中的小时 (0 ~ 23)。
- setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。
- setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。
- setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。
- setTime() 以毫秒设置 Date 对象。
四:时间转字符串
-
toString() 把 Date 对象转换为字符串。Wed Nov 25 2020 20:45:21 GMT+0800 (中国标准时间)
-
toTimeString() 把 Date 对象的时间部分转换为字符串。20:45:34 GMT+0800 (中国标准时间)
-
toDateString() 把 Date 对象的日期部分转换为字符串。Wed Nov 25 2020
-
toUTCString() 根据世界时,把 Date 对象转换为字符串。Wed, 25 Nov 2020 12:57:50 GMT
-
toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。2020/11/25 下午8:50:58
-
toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。下午8:51:04
-
toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 2020/11/25
-
获取今天刚开始的时间戳:new Date(new Date().toLocaleDateString()).getTime()