语法:
new Date(); new Date(time);
time的格式可以是:
1.2022,10,00,00,00,00(年,月,日,时,分,秒);
2.2022/10/00 00:00:00 (这个必须加引号)
其他的格式可能会显示错误,不推荐。
可以通过replace替换/
创建一个新Date对象的方法是通过new操作符,如果不加new则是返回一个字符串。
如果不以new的形式来调用Date对象,则会返回一个当时日期和是时间的字符串,如:Date.now() Date.parse() Date.UTC()
如果创建了new Date对象,所拥有的方法有:
根据本地时间:
- getDate() 返回一个指定的Date对象的日期(1-31);
- getDay() 返回星期几(0-6);
- getFullYear() 返回年;
- getHours() 返回小时(0-23);
- getMinutes() 返回分钟数(0-59);
- getMonth() 返回月份(0-11);
- getSeconds() 返回秒数(0-59);
- setDate() 设置一个Date对象所属月份的天数;
- setFullYear() 设置年;
- setHour() 设置小时数
- setMilliseconds() 设置毫秒数
- setMinutes() 设置分钟数
- setMonth() 设置月份
- setSeconds() 设置秒数 ...等等
不是根据本地时间:
- getTime() 返回从1970.1.1 00:00:00(UTC,协调世界时) 到目前为止的时间的毫秒数 --- 更早的时间会用负数表示;
- getTimezoneOffset() 返回UTC相对于现在是简单差值,单位为分钟;
- getYear() 返回一个相对UTC的相对年份
获得当前时间:
/**
* 获取当前时间,精确到分秒
*/
getNowDay() {
let date = new Date();
let year = date.getFullYear();
let mouth = date.getMonth() + 1;
let day = date.getDate();
let hours = date.getHours(); //获取当前小时
let minutes = date.getMinutes(); //获取当前分钟
let seconds = date.getSeconds();
return `${year}-${mouth}-${day} ${hours}:${minutes}:${seconds}`;
},