Date日期内置对象
创建新的Date对象
- 必须通过new操作符,否则会将Date作为常规函数调用,返回一个字符串,而不是一个Date对象
new Date()
括号内的值:- 无参数:新创建的Date对象表示实例化时刻的时间和日期
属性
- 允许添加属性
Date.length
值为7,表示该函数可接受的参数个数
方法
Date.now()
返回一个1970年1月1日00:00到现在为止的时间戳dateObj.getDate() // 返回一个1到31的整数
实例
2:获取当月的时间——年月日格式
getMonthString(date){
var fullYear = date.getFullYear();
var month = date.getMonth()+1;
var endOfMonth = new Date(fullYear,month,0);
var startOfMonth = new Date(date.setDate(1));
//此时可以获取到月初和月末的标准时间格式,想要转化为年月日,则需要使用上面的函数了
var startTime = this.getDateString(startOfMonth);
var endTime = this.getDateString(endOfMonth);
return [startTime,endTime];
}