概述
JS中的 Date 对象用于处理日期和时间;Date 对象和 Math 对象不一样,Date 是一个构造函数,需要实例化后才能使用对象中具体的方法和属性。
创建 Date 对象的方法
创建当前(现在)日期对象的实例,不带任何参数
var today = new Date();
console.log(today); // Tue Nov 05 2019 20:43:15 GMT+0800 (中国标准时间)
创建指定时间戳的日期对象实例,参数是时间戳。
时间戳:是指某一个时间距离1970年1月1日0时0分0秒,过去了多少毫秒值(1秒=1000毫秒)。
var timer = new Date(10000); //格林威治时间是 1970年1月1日0时0分10秒;北京比格林威治早八小时
console.log(timer); // Thu Jan 01 1970 08:00:10 GMT+0800 (中国标准时间)
指定一个字符串的日期时间信息
对象中的参数是一个日期时间字符串
var timer = new Date("2099/5/25 10:00:00");
console.log(timer); // Mon May 25 2099 10:00:00 GMT+0800 (中国标准时间)
指定多个数值参数
可以指定多个参数,多个参数用逗号隔开;顺序为:年、月(月份是从 0 开始)、日、时、分、秒;其中年、月、日是必须的。
var timer = new Date(2019+100,4,25,10,20,0);
console.log(timer); // Thu May 25 2119 10:20:00 GMT+0800 (中国标准时间)
当 Date 作为构造函数调用并传入多个参数时,如果数值大于合理范围时(如月份为13或者分钟数为70),相邻的数值会被调整。比如 new Date(2019, 13, 1)等于new Date(2020, 1, 1),它们都表示日期2020-02-01(注意月份是从0开始的)。
其他数值也是类似,new Date(2019, 2, 1, 0, 70)等于new Date(2019, 2, 1, 1, 10),都表示时间2019-03-01T01:10:00
需要注意的是只能通过调用 Date 构造函数来实例化日期对象;若以常规函数调用它(即不加 new 操作符)将会返回一个字符串,而不是一个日期对象。另外,不像其他 JavaScript 类型,Date 对象没有字面量格式
Date 中常用的方法
getFullYear()
dateObject.getFullYear()
获取当前日期中的年份(四位)
dateObject
创建的日期对象
// 创建指定时间的日期对象
var today = new Date('2019/11/11 08:36:16');
// 获取对象中的年份
var year = today.getFullYear();
console.log(year); // 2019
getMonth()
dateObject.getMonth()
获取月份(0-11 月)
// 获取对象中的月份
var month = today.getMonth(); // 实际的月份要加一
console.log(month); // 10
getDate()
dateObject.getDate()
获取当月的第几天(1 ~ 31)
var day = today.getDate();
console.log(day); // 11
getDay()
dateObject.getDay()
获取星期值(一周中的某一天);0为周日(0 ~ 6)
var week = today.getDay();
console.log(week); // 1
getHours()
dateObject.getHours()
获得当前时间的小时(0 ~ 23)
var hours = today.getHours();
console.log(hours); // 8
getMinutes()
dateObject.getMinutes()
获得当前时间的分钟(0 ~ 59)
var minutes = today.getMinutes();
console.log(minutes); // 36
getSeconds()
dateObject.getSeconds()
获得当前时间秒数
var seconds = today.getSeconds();
console.log(seconds); // 16
getMilliSeconds()
dateObject.getMilliSeconds()
获取当前时间毫秒数
var milliseconds = today.getMilliseconds();
console.log(milliseconds); // 0
valueOf()
dateObject.velueOf()
返回从1970年1月1日0时0分0秒到该日期对象所代表时间的毫秒数。
var mil = today.valueOf();
console.log(mil); // 1573432576000
getTime()
dateObject.getTime()
获得1970年1月1日距离指定日期的毫秒值
var time = today.getTime();
console.log(time); // 1573432576000
另一种获取毫秒值的方法
在创建日期对象时在 new
关键词之前添加一个加号 +
,就可以直接获取指定的日期距 1970年1月1日 的毫秒值(返回的是毫秒值)
var today = +new Date('2019/11/11 08:36:16');
console.log(today); // 1573432576000
H5新增获取毫秒值的方法
H5 新增了获取当前时间距离 1970年1月1日 的毫秒值的方法: Date.now()
,该方法没有参数,所以获得的永远是当前时间距1970年1月1日 的毫秒值;因为是新增的所以存在兼容性问题
console.log(Date.now()) // 1573008869362
以下方法不常用
toDateString()
dateObject.toDateString()
英文格式的日期
var english = today.toDateString();
console.log(english); // Mon Nov 11 2019
toLocaleDateString()
dateObject.toLocaleDateString()
根据本地时间把 Date 对象的日期部分转换为字符串,并返回结果
var china = today.toLocaleDateString();
console.log(china); // 2019/11/11
toTimeString()
dateObject.toTimeString()
方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。
var eng = today.toTimeString();
console.log(eng); // 08:36:16 GMT+0800 (中国标准时间)
toLocaleTimeString()
dateObject.toLocaleTimeString()
返回一个使用默认语言环境和格式设置的格式化时间
var chi = today.toLocaleTimeString();
console.log(chi); // 上午8:36:16