有那些做自媒体短视频的网站,做个网站成功案例,网站类游戏网站开发,企业年度报告公示系统日期对象
Date对象 Date 对象和 Math 对象不一样#xff0c;他是一个构造函数#xff0c;所以我们需要实例化后才能使用 Date 实例用来处理日期和时间
Date()使用方法
示例#xff1a;获取当前时间
let now new Date()
console.log(now) 示例#xff1a;获取指定时间…日期对象
Date对象 Date 对象和 Math 对象不一样他是一个构造函数所以我们需要实例化后才能使用 Date 实例用来处理日期和时间
Date()使用方法
示例获取当前时间
let now new Date()
console.log(now) 示例获取指定时间
Date() 构造函数的参数可以设置指定的时间
let now new Date(2000-10-10) //指定日期
console.log(now) //Tue Oct 10 2000 08:00:00 GMT0800 (中国标准时间)
now new Date(2000/10/10) //指定日期
console.log(now) //Tue Oct 10 2000 08:00:00 GMT0800 (中国标准时间)
now new Date(2000.10.10) //指定日期
console.log(now) //Tue Oct 10 2000 08:00:00 GMT0800 (中国标准时间)日期格式化
需要获取日期指定的部分我们要手动得到这种格式
方法名说明代码getFullYear()获取当年dObj.getFullYear()getMonth()获取当月0-11dObj.getMonth()getDate获取当天日期dObj.getDate()getDay获取星期几周日0 到 周六6dObj.getDay()getHours()获取当前小时dObj.getHours()getMinutes()获取当前分钟dObj.getMinutes()getSeconds()获取当前秒钟dObj.getSeconds()setFullYear(数字)设置日期中的年dObj.setFullYear(2000)setMonth(数字)设置日期中的月0-11dObj.setMonth(1)setDate(数字)设置日期中的天dObj.setDate(1)setHours(数字)设置日期中的小时dObj.setHours(11)setMinutes(数字)设置日期中的分钟dObj.setMinutes(12)setSeconds(数字)设置日期中的秒钟dObj.setSeconds(13)setTime(数字)设置日期中的时间戳dObj.setTime(1660449716531)
示例
let now new Date(2000-10-10 11:12:13) //指定日期
let fullyear now.getFullYear()
let month now.getMonth() 1
let date now.getDate()
let day now.getDay()
let hour now.getHours()
let minute now.getMinutes()
let second now.getSeconds()
let res 现在是${fullyear}年${month}月${date}日${hour}点${minute}分${second}秒星期${day}
console.log(res) // 现在是2000年10月10日11点12分13秒星期2
时间戳
时间戳是指 从1970年1月1日世界标准时间起到现在的毫秒数。 示例 获取时间戳
// 方法1. 通过 valueOf()
let date new Date()
console.log(date.valueOf()); // 现在距离1970.1.1 的时间戳
// 方法2. 通过 getTime()
console.log(date.getTime()); // 现在距离1970.1.1 的时间戳
// 方法3. new Date() 简单的写法
let date1 new Date()
console.log(date1); // 现在距离1970.1.1 的时间戳
// 方法4. ES6 新增的 获得总的毫秒数
console.log(Date.now()); // 现在距离1970.1.1 的时间戳